]> git.sesse.net Git - ffmpeg/blob - libavformat/network.c
Merge commit '5d01bd181bb77e6740462095d7be4e0733a59420'
[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             break;
170     } while (timeout <= 0 || runs-- > 0);
171
172     if (!ret)
173         return AVERROR(ETIMEDOUT);
174     if (ret < 0)
175         return ff_neterrno();
176     return ret;
177 }
178
179 int ff_socket(int af, int type, int proto)
180 {
181     int fd;
182
183 #ifdef SOCK_CLOEXEC
184     fd = socket(af, type | SOCK_CLOEXEC, proto);
185     if (fd == -1 && errno == EINVAL)
186 #endif
187     {
188         fd = socket(af, type, proto);
189 #if HAVE_FCNTL
190         if (fd != -1) {
191             if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
192                 av_log(NULL, AV_LOG_DEBUG, "Failed to set close on exec\n");
193         }
194 #endif
195     }
196 #ifdef SO_NOSIGPIPE
197     if (fd != -1) {
198         if (setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &(int){1}, sizeof(int))) {
199              av_log(NULL, AV_LOG_WARNING, "setsockopt(SO_NOSIGPIPE) failed\n");
200         }
201     }
202 #endif
203     return fd;
204 }
205
206 int ff_listen(int fd, const struct sockaddr *addr,
207               socklen_t addrlen)
208 {
209     int ret;
210     int reuse = 1;
211     if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse))) {
212         av_log(NULL, AV_LOG_WARNING, "setsockopt(SO_REUSEADDR) failed\n");
213     }
214     ret = bind(fd, addr, addrlen);
215     if (ret)
216         return ff_neterrno();
217
218     ret = listen(fd, 1);
219     if (ret)
220         return ff_neterrno();
221     return ret;
222 }
223
224 int ff_accept(int fd, int timeout, URLContext *h)
225 {
226     int ret;
227     struct pollfd lp = { fd, POLLIN, 0 };
228
229     ret = ff_poll_interrupt(&lp, 1, timeout, &h->interrupt_callback);
230     if (ret < 0)
231         return ret;
232
233     ret = accept(fd, NULL, NULL);
234     if (ret < 0)
235         return ff_neterrno();
236     if (ff_socket_nonblock(ret, 1) < 0)
237         av_log(NULL, AV_LOG_DEBUG, "ff_socket_nonblock failed\n");
238
239     return ret;
240 }
241
242 int ff_listen_bind(int fd, const struct sockaddr *addr,
243                    socklen_t addrlen, int timeout, URLContext *h)
244 {
245     int ret;
246     if ((ret = ff_listen(fd, addr, addrlen)) < 0)
247         return ret;
248     if ((ret = ff_accept(fd, timeout, h)) < 0)
249         return ret;
250     closesocket(fd);
251     return ret;
252 }
253
254 int ff_listen_connect(int fd, const struct sockaddr *addr,
255                       socklen_t addrlen, int timeout, URLContext *h,
256                       int will_try_next)
257 {
258     struct pollfd p = {fd, POLLOUT, 0};
259     int ret;
260     socklen_t optlen;
261
262     if (ff_socket_nonblock(fd, 1) < 0)
263         av_log(NULL, AV_LOG_DEBUG, "ff_socket_nonblock failed\n");
264
265     while ((ret = connect(fd, addr, addrlen))) {
266         ret = ff_neterrno();
267         switch (ret) {
268         case AVERROR(EINTR):
269             if (ff_check_interrupt(&h->interrupt_callback))
270                 return AVERROR_EXIT;
271             continue;
272         case AVERROR(EINPROGRESS):
273         case AVERROR(EAGAIN):
274             ret = ff_poll_interrupt(&p, 1, timeout, &h->interrupt_callback);
275             if (ret < 0)
276                 return ret;
277             optlen = sizeof(ret);
278             if (getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen))
279                 ret = AVUNERROR(ff_neterrno());
280             if (ret != 0) {
281                 char errbuf[100];
282                 ret = AVERROR(ret);
283                 av_strerror(ret, errbuf, sizeof(errbuf));
284                 if (will_try_next)
285                     av_log(h, AV_LOG_WARNING,
286                            "Connection to %s failed (%s), trying next address\n",
287                            h->filename, errbuf);
288                 else
289                     av_log(h, AV_LOG_ERROR, "Connection to %s failed: %s\n",
290                            h->filename, errbuf);
291             }
292         default:
293             return ret;
294         }
295     }
296     return ret;
297 }
298
299 static int match_host_pattern(const char *pattern, const char *hostname)
300 {
301     int len_p, len_h;
302     if (!strcmp(pattern, "*"))
303         return 1;
304     // Skip a possible *. at the start of the pattern
305     if (pattern[0] == '*')
306         pattern++;
307     if (pattern[0] == '.')
308         pattern++;
309     len_p = strlen(pattern);
310     len_h = strlen(hostname);
311     if (len_p > len_h)
312         return 0;
313     // Simply check if the end of hostname is equal to 'pattern'
314     if (!strcmp(pattern, &hostname[len_h - len_p])) {
315         if (len_h == len_p)
316             return 1; // Exact match
317         if (hostname[len_h - len_p - 1] == '.')
318             return 1; // The matched substring is a domain and not just a substring of a domain
319     }
320     return 0;
321 }
322
323 int ff_http_match_no_proxy(const char *no_proxy, const char *hostname)
324 {
325     char *buf, *start;
326     int ret = 0;
327     if (!no_proxy)
328         return 0;
329     if (!hostname)
330         return 0;
331     buf = av_strdup(no_proxy);
332     if (!buf)
333         return 0;
334     start = buf;
335     while (start) {
336         char *sep, *next = NULL;
337         start += strspn(start, " ,");
338         sep = start + strcspn(start, " ,");
339         if (*sep) {
340             next = sep + 1;
341             *sep = '\0';
342         }
343         if (match_host_pattern(start, hostname)) {
344             ret = 1;
345             break;
346         }
347         start = next;
348     }
349     av_free(buf);
350     return ret;
351 }
352
353 void ff_log_net_error(void *ctx, int level, const char* prefix)
354 {
355     char errbuf[100];
356     av_strerror(ff_neterrno(), errbuf, sizeof(errbuf));
357     av_log(ctx, level, "%s: %s\n", prefix, errbuf);
358 }