]> git.sesse.net Git - ffmpeg/blob - libavformat/network.c
Merge commit 'bddb2ce179c57db6e3c79fdc3363c165d90850b0'
[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_inited_globally;
58
59 int ff_network_init(void)
60 {
61 #if HAVE_WINSOCK2_H
62     WSADATA wsaData;
63 #endif
64
65     if (!ff_network_inited_globally)
66         av_log(NULL, AV_LOG_WARNING, "Using network protocols without global "
67                                      "network initialization. Please use "
68                                      "avformat_network_init(), this will "
69                                      "become mandatory later.\n");
70 #if HAVE_WINSOCK2_H
71     if (WSAStartup(MAKEWORD(1,1), &wsaData))
72         return 0;
73 #endif
74     return 1;
75 }
76
77 int ff_network_wait_fd(int fd, int write)
78 {
79     int ev = write ? POLLOUT : POLLIN;
80     struct pollfd p = { .fd = fd, .events = ev, .revents = 0 };
81     int ret;
82     ret = poll(&p, 1, POLLING_TIME);
83     return ret < 0 ? ff_neterrno() : p.revents & (ev | POLLERR | POLLHUP) ? 0 : AVERROR(EAGAIN);
84 }
85
86 int ff_network_wait_fd_timeout(int fd, int write, int64_t timeout, AVIOInterruptCB *int_cb)
87 {
88     int ret;
89     int64_t wait_start = 0;
90
91     while (1) {
92         if (ff_check_interrupt(int_cb))
93             return AVERROR_EXIT;
94         ret = ff_network_wait_fd(fd, write);
95         if (ret != AVERROR(EAGAIN))
96             return ret;
97         if (timeout > 0) {
98             if (!wait_start)
99                 wait_start = av_gettime_relative();
100             else if (av_gettime_relative() - wait_start > timeout)
101                 return AVERROR(ETIMEDOUT);
102         }
103     }
104 }
105
106 void ff_network_close(void)
107 {
108 #if HAVE_WINSOCK2_H
109     WSACleanup();
110 #endif
111 }
112
113 #if HAVE_WINSOCK2_H
114 int ff_neterrno(void)
115 {
116     int err = WSAGetLastError();
117     switch (err) {
118     case WSAEWOULDBLOCK:
119         return AVERROR(EAGAIN);
120     case WSAEINTR:
121         return AVERROR(EINTR);
122     case WSAEPROTONOSUPPORT:
123         return AVERROR(EPROTONOSUPPORT);
124     case WSAETIMEDOUT:
125         return AVERROR(ETIMEDOUT);
126     case WSAECONNREFUSED:
127         return AVERROR(ECONNREFUSED);
128     case WSAEINPROGRESS:
129         return AVERROR(EINPROGRESS);
130     }
131     return -err;
132 }
133 #endif
134
135 int ff_is_multicast_address(struct sockaddr *addr)
136 {
137     if (addr->sa_family == AF_INET) {
138         return IN_MULTICAST(ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr));
139     }
140 #if HAVE_STRUCT_SOCKADDR_IN6
141     if (addr->sa_family == AF_INET6) {
142         return IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)addr)->sin6_addr);
143     }
144 #endif
145
146     return 0;
147 }
148
149 static int ff_poll_interrupt(struct pollfd *p, nfds_t nfds, int timeout,
150                              AVIOInterruptCB *cb)
151 {
152     int runs = timeout / POLLING_TIME;
153     int ret = 0;
154
155     do {
156         if (ff_check_interrupt(cb))
157             return AVERROR_EXIT;
158         ret = poll(p, nfds, POLLING_TIME);
159         if (ret != 0)
160             break;
161     } while (timeout <= 0 || runs-- > 0);
162
163     if (!ret)
164         return AVERROR(ETIMEDOUT);
165     if (ret < 0)
166         return ff_neterrno();
167     return ret;
168 }
169
170 int ff_socket(int af, int type, int proto)
171 {
172     int fd;
173
174 #ifdef SOCK_CLOEXEC
175     fd = socket(af, type | SOCK_CLOEXEC, proto);
176     if (fd == -1 && errno == EINVAL)
177 #endif
178     {
179         fd = socket(af, type, proto);
180 #if HAVE_FCNTL
181         if (fd != -1) {
182             if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
183                 av_log(NULL, AV_LOG_DEBUG, "Failed to set close on exec\n");
184         }
185 #endif
186     }
187 #ifdef SO_NOSIGPIPE
188     if (fd != -1)
189         setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &(int){1}, sizeof(int));
190 #endif
191     return fd;
192 }
193
194 int ff_listen(int fd, const struct sockaddr *addr,
195               socklen_t addrlen)
196 {
197     int ret;
198     int reuse = 1;
199     if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse))) {
200         av_log(NULL, AV_LOG_WARNING, "setsockopt(SO_REUSEADDR) failed\n");
201     }
202     ret = bind(fd, addr, addrlen);
203     if (ret)
204         return ff_neterrno();
205
206     ret = listen(fd, 1);
207     if (ret)
208         return ff_neterrno();
209     return ret;
210 }
211
212 int ff_accept(int fd, int timeout, URLContext *h)
213 {
214     int ret;
215     struct pollfd lp = { fd, POLLIN, 0 };
216
217     ret = ff_poll_interrupt(&lp, 1, timeout, &h->interrupt_callback);
218     if (ret < 0)
219         return ret;
220
221     ret = accept(fd, NULL, NULL);
222     if (ret < 0)
223         return ff_neterrno();
224     if (ff_socket_nonblock(ret, 1) < 0)
225         av_log(NULL, AV_LOG_DEBUG, "ff_socket_nonblock failed\n");
226
227     return ret;
228 }
229
230 int ff_listen_bind(int fd, const struct sockaddr *addr,
231                    socklen_t addrlen, int timeout, URLContext *h)
232 {
233     int ret;
234     if ((ret = ff_listen(fd, addr, addrlen)) < 0)
235         return ret;
236     if ((ret = ff_accept(fd, timeout, h)) < 0)
237         return ret;
238     closesocket(fd);
239     return ret;
240 }
241
242 int ff_listen_connect(int fd, const struct sockaddr *addr,
243                       socklen_t addrlen, int timeout, URLContext *h,
244                       int will_try_next)
245 {
246     struct pollfd p = {fd, POLLOUT, 0};
247     int ret;
248     socklen_t optlen;
249
250     if (ff_socket_nonblock(fd, 1) < 0)
251         av_log(NULL, AV_LOG_DEBUG, "ff_socket_nonblock failed\n");
252
253     while ((ret = connect(fd, addr, addrlen))) {
254         ret = ff_neterrno();
255         switch (ret) {
256         case AVERROR(EINTR):
257             if (ff_check_interrupt(&h->interrupt_callback))
258                 return AVERROR_EXIT;
259             continue;
260         case AVERROR(EINPROGRESS):
261         case AVERROR(EAGAIN):
262             ret = ff_poll_interrupt(&p, 1, timeout, &h->interrupt_callback);
263             if (ret < 0)
264                 return ret;
265             optlen = sizeof(ret);
266             if (getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen))
267                 ret = AVUNERROR(ff_neterrno());
268             if (ret != 0) {
269                 char errbuf[100];
270                 ret = AVERROR(ret);
271                 av_strerror(ret, errbuf, sizeof(errbuf));
272                 if (will_try_next)
273                     av_log(h, AV_LOG_WARNING,
274                            "Connection to %s failed (%s), trying next address\n",
275                            h->filename, errbuf);
276                 else
277                     av_log(h, AV_LOG_ERROR, "Connection to %s failed: %s\n",
278                            h->filename, errbuf);
279             }
280         default:
281             return ret;
282         }
283     }
284     return ret;
285 }
286
287 static int match_host_pattern(const char *pattern, const char *hostname)
288 {
289     int len_p, len_h;
290     if (!strcmp(pattern, "*"))
291         return 1;
292     // Skip a possible *. at the start of the pattern
293     if (pattern[0] == '*')
294         pattern++;
295     if (pattern[0] == '.')
296         pattern++;
297     len_p = strlen(pattern);
298     len_h = strlen(hostname);
299     if (len_p > len_h)
300         return 0;
301     // Simply check if the end of hostname is equal to 'pattern'
302     if (!strcmp(pattern, &hostname[len_h - len_p])) {
303         if (len_h == len_p)
304             return 1; // Exact match
305         if (hostname[len_h - len_p - 1] == '.')
306             return 1; // The matched substring is a domain and not just a substring of a domain
307     }
308     return 0;
309 }
310
311 int ff_http_match_no_proxy(const char *no_proxy, const char *hostname)
312 {
313     char *buf, *start;
314     int ret = 0;
315     if (!no_proxy)
316         return 0;
317     if (!hostname)
318         return 0;
319     buf = av_strdup(no_proxy);
320     if (!buf)
321         return 0;
322     start = buf;
323     while (start) {
324         char *sep, *next = NULL;
325         start += strspn(start, " ,");
326         sep = start + strcspn(start, " ,");
327         if (*sep) {
328             next = sep + 1;
329             *sep = '\0';
330         }
331         if (match_host_pattern(start, hostname)) {
332             ret = 1;
333             break;
334         }
335         start = next;
336     }
337     av_free(buf);
338     return ret;
339 }