network: Use ff_neterrno instead of AVERROR(errno) for poll errors
[libav.git] / libavformat / network.c
CommitLineData
f6d37100
MS
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
9d5ec50e 21#include <fcntl.h>
f6d37100 22#include "network.h"
57cde2b1 23#include "tls.h"
f849a77e 24#include "url.h"
48e59ad8 25#include "libavcodec/internal.h"
70766c21 26#include "libavutil/mem.h"
48e59ad8 27
48e59ad8
MS
28void ff_tls_init(void)
29{
61cec5ad
DB
30#if CONFIG_TLS_PROTOCOL
31#if CONFIG_OPENSSL
57cde2b1 32 ff_openssl_init();
48e59ad8 33#endif
61cec5ad 34#if CONFIG_GNUTLS
57cde2b1 35 ff_gnutls_init();
48e59ad8 36#endif
61cec5ad 37#endif
48e59ad8
MS
38}
39
40void ff_tls_deinit(void)
41{
61cec5ad
DB
42#if CONFIG_TLS_PROTOCOL
43#if CONFIG_OPENSSL
57cde2b1 44 ff_openssl_deinit();
48e59ad8 45#endif
61cec5ad 46#if CONFIG_GNUTLS
57cde2b1 47 ff_gnutls_deinit();
1606e551 48#endif
61cec5ad 49#endif
48e59ad8 50}
f6d37100 51
ce145690
MS
52int ff_network_inited_globally;
53
f6d37100
MS
54int ff_network_init(void)
55{
139cef8e
SR
56#if HAVE_WINSOCK2_H
57 WSADATA wsaData;
58#endif
59
ce145690
MS
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");
f6d37100 65#if HAVE_WINSOCK2_H
f6d37100
MS
66 if (WSAStartup(MAKEWORD(1,1), &wsaData))
67 return 0;
68#endif
69 return 1;
70}
71
72int 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
81void ff_network_close(void)
82{
83#if HAVE_WINSOCK2_H
84 WSACleanup();
85#endif
86}
87
88#if HAVE_WINSOCK2_H
89int 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);
4b1b1449
RB
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);
f6d37100
MS
105 }
106 return -err;
107}
108#endif
109
110int 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}
28306e6d 123
9835abb6
LB
124static 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)
c194b9ad 141 return ff_neterrno();
9835abb6
LB
142 return ret;
143}
144
9d5ec50e
RDC
145int 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 }
881b80b3 160#ifdef SO_NOSIGPIPE
161 if (fd != -1)
162 setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &(int){1}, sizeof(int));
163#endif
9d5ec50e
RDC
164 return fd;
165}
166
28306e6d 167int ff_listen_bind(int fd, const struct sockaddr *addr,
9835abb6 168 socklen_t addrlen, int timeout, URLContext *h)
28306e6d
LB
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
9835abb6
LB
182 ret = ff_poll_interrupt(&lp, 1, timeout, &h->interrupt_callback);
183 if (ret < 0)
184 return ret;
28306e6d
LB
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}
f849a77e
LB
195
196int ff_listen_connect(int fd, const struct sockaddr *addr,
abe5268c
MS
197 socklen_t addrlen, int timeout, URLContext *h,
198 int will_try_next)
f849a77e
LB
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):
9835abb6
LB
215 ret = ff_poll_interrupt(&p, 1, timeout, &h->interrupt_callback);
216 if (ret < 0)
217 return ret;
f849a77e
LB
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));
abe5268c
MS
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);
f849a77e
LB
232 }
233 default:
234 return ret;
235 }
236 }
237 return ret;
238}
ec7c51c7
LB
239
240static 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
264int 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}