86d79553f7d7769b2a087b27722ee94f1d8b1847
[libav.git] / 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 <fcntl.h>
22 #include "network.h"
23 #include "tls.h"
24 #include "url.h"
25 #include "libavcodec/internal.h"
26 #include "libavutil/mem.h"
27
28 void ff_tls_init(void)
29 {
30 #if CONFIG_TLS_PROTOCOL
31 #if CONFIG_OPENSSL
32 ff_openssl_init();
33 #endif
34 #if CONFIG_GNUTLS
35 ff_gnutls_init();
36 #endif
37 #endif
38 }
39
40 void ff_tls_deinit(void)
41 {
42 #if CONFIG_TLS_PROTOCOL
43 #if CONFIG_OPENSSL
44 ff_openssl_deinit();
45 #endif
46 #if CONFIG_GNUTLS
47 ff_gnutls_deinit();
48 #endif
49 #endif
50 }
51
52 int ff_network_inited_globally;
53
54 int ff_network_init(void)
55 {
56 #if HAVE_WINSOCK2_H
57 WSADATA wsaData;
58 #endif
59
60 if (!ff_network_inited_globally)
61 av_log(NULL, AV_LOG_WARNING, "Using network protocols without global "
62 "network initialization. Please use "
63 "avformat_network_init(), this will "
64 "become mandatory later.\n");
65 #if HAVE_WINSOCK2_H
66 if (WSAStartup(MAKEWORD(1,1), &wsaData))
67 return 0;
68 #endif
69 return 1;
70 }
71
72 int ff_network_wait_fd(int fd, int write)
73 {
74 int ev = write ? POLLOUT : POLLIN;
75 struct pollfd p = { .fd = fd, .events = ev, .revents = 0 };
76 int ret;
77 ret = poll(&p, 1, 100);
78 return ret < 0 ? ff_neterrno() : p.revents & (ev | POLLERR | POLLHUP) ? 0 : AVERROR(EAGAIN);
79 }
80
81 void ff_network_close(void)
82 {
83 #if HAVE_WINSOCK2_H
84 WSACleanup();
85 #endif
86 }
87
88 #if HAVE_WINSOCK2_H
89 int ff_neterrno(void)
90 {
91 int err = WSAGetLastError();
92 switch (err) {
93 case WSAEWOULDBLOCK:
94 return AVERROR(EAGAIN);
95 case WSAEINTR:
96 return AVERROR(EINTR);
97 case WSAEPROTONOSUPPORT:
98 return AVERROR(EPROTONOSUPPORT);
99 case WSAETIMEDOUT:
100 return AVERROR(ETIMEDOUT);
101 case WSAECONNREFUSED:
102 return AVERROR(ECONNREFUSED);
103 case WSAEINPROGRESS:
104 return AVERROR(EINPROGRESS);
105 }
106 return -err;
107 }
108 #endif
109
110 int ff_is_multicast_address(struct sockaddr *addr)
111 {
112 if (addr->sa_family == AF_INET) {
113 return IN_MULTICAST(ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr));
114 }
115 #if HAVE_STRUCT_SOCKADDR_IN6
116 if (addr->sa_family == AF_INET6) {
117 return IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)addr)->sin6_addr);
118 }
119 #endif
120
121 return 0;
122 }
123
124 static int ff_poll_interrupt(struct pollfd *p, nfds_t nfds, int timeout,
125 AVIOInterruptCB *cb)
126 {
127 int runs = timeout / POLLING_TIME;
128 int ret = 0;
129
130 do {
131 if (ff_check_interrupt(cb))
132 return AVERROR_EXIT;
133 ret = poll(p, nfds, POLLING_TIME);
134 if (ret != 0)
135 break;
136 } while (timeout < 0 || runs-- > 0);
137
138 if (!ret)
139 return AVERROR(ETIMEDOUT);
140 if (ret < 0)
141 return AVERROR(errno);
142 return ret;
143 }
144
145 int ff_socket(int af, int type, int proto)
146 {
147 int fd;
148
149 #ifdef SOCK_CLOEXEC
150 fd = socket(af, type | SOCK_CLOEXEC, proto);
151 if (fd == -1 && errno == EINVAL)
152 #endif
153 {
154 fd = socket(af, type, proto);
155 #if HAVE_FCNTL
156 if (fd != -1)
157 fcntl(fd, F_SETFD, FD_CLOEXEC);
158 #endif
159 }
160 #ifdef SO_NOSIGPIPE
161 if (fd != -1)
162 setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &(int){1}, sizeof(int));
163 #endif
164 return fd;
165 }
166
167 int ff_listen_bind(int fd, const struct sockaddr *addr,
168 socklen_t addrlen, int timeout, URLContext *h)
169 {
170 int ret;
171 int reuse = 1;
172 struct pollfd lp = { fd, POLLIN, 0 };
173 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
174 ret = bind(fd, addr, addrlen);
175 if (ret)
176 return ff_neterrno();
177
178 ret = listen(fd, 1);
179 if (ret)
180 return ff_neterrno();
181
182 ret = ff_poll_interrupt(&lp, 1, timeout, &h->interrupt_callback);
183 if (ret < 0)
184 return ret;
185
186 ret = accept(fd, NULL, NULL);
187 if (ret < 0)
188 return ff_neterrno();
189
190 closesocket(fd);
191
192 ff_socket_nonblock(ret, 1);
193 return ret;
194 }
195
196 int ff_listen_connect(int fd, const struct sockaddr *addr,
197 socklen_t addrlen, int timeout, URLContext *h,
198 int will_try_next)
199 {
200 struct pollfd p = {fd, POLLOUT, 0};
201 int ret;
202 socklen_t optlen;
203
204 ff_socket_nonblock(fd, 1);
205
206 while ((ret = connect(fd, addr, addrlen))) {
207 ret = ff_neterrno();
208 switch (ret) {
209 case AVERROR(EINTR):
210 if (ff_check_interrupt(&h->interrupt_callback))
211 return AVERROR_EXIT;
212 continue;
213 case AVERROR(EINPROGRESS):
214 case AVERROR(EAGAIN):
215 ret = ff_poll_interrupt(&p, 1, timeout, &h->interrupt_callback);
216 if (ret < 0)
217 return ret;
218 optlen = sizeof(ret);
219 if (getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen))
220 ret = AVUNERROR(ff_neterrno());
221 if (ret != 0) {
222 char errbuf[100];
223 ret = AVERROR(ret);
224 av_strerror(ret, errbuf, sizeof(errbuf));
225 if (will_try_next)
226 av_log(h, AV_LOG_WARNING,
227 "Connection to %s failed (%s), trying next address\n",
228 h->filename, errbuf);
229 else
230 av_log(h, AV_LOG_ERROR, "Connection to %s failed: %s\n",
231 h->filename, errbuf);
232 }
233 default:
234 return ret;
235 }
236 }
237 return ret;
238 }
239
240 static int match_host_pattern(const char *pattern, const char *hostname)
241 {
242 int len_p, len_h;
243 if (!strcmp(pattern, "*"))
244 return 1;
245 // Skip a possible *. at the start of the pattern
246 if (pattern[0] == '*')
247 pattern++;
248 if (pattern[0] == '.')
249 pattern++;
250 len_p = strlen(pattern);
251 len_h = strlen(hostname);
252 if (len_p > len_h)
253 return 0;
254 // Simply check if the end of hostname is equal to 'pattern'
255 if (!strcmp(pattern, &hostname[len_h - len_p])) {
256 if (len_h == len_p)
257 return 1; // Exact match
258 if (hostname[len_h - len_p - 1] == '.')
259 return 1; // The matched substring is a domain and not just a substring of a domain
260 }
261 return 0;
262 }
263
264 int ff_http_match_no_proxy(const char *no_proxy, const char *hostname)
265 {
266 char *buf, *start;
267 int ret = 0;
268 if (!no_proxy)
269 return 0;
270 if (!hostname)
271 return 0;
272 buf = av_strdup(no_proxy);
273 if (!buf)
274 return 0;
275 start = buf;
276 while (start) {
277 char *sep, *next = NULL;
278 start += strspn(start, " ,");
279 sep = start + strcspn(start, " ,");
280 if (*sep) {
281 next = sep + 1;
282 *sep = '\0';
283 }
284 if (match_host_pattern(start, hostname)) {
285 ret = 1;
286 break;
287 }
288 start = next;
289 }
290 av_free(buf);
291 return ret;
292 }