FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
network.c
Go to the documentation of this file.
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
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
50 #endif
51 #if CONFIG_GNUTLS
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 
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 
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,
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  setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &(int){1}, sizeof(int));
199 #endif
200  return fd;
201 }
202 
203 int ff_listen(int fd, const struct sockaddr *addr,
204  socklen_t addrlen)
205 {
206  int ret;
207  int reuse = 1;
208  if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse))) {
209  av_log(NULL, AV_LOG_WARNING, "setsockopt(SO_REUSEADDR) failed\n");
210  }
211  ret = bind(fd, addr, addrlen);
212  if (ret)
213  return ff_neterrno();
214 
215  ret = listen(fd, 1);
216  if (ret)
217  return ff_neterrno();
218  return ret;
219 }
220 
221 int ff_accept(int fd, int timeout, URLContext *h)
222 {
223  int ret;
224  struct pollfd lp = { fd, POLLIN, 0 };
225 
226  ret = ff_poll_interrupt(&lp, 1, timeout, &h->interrupt_callback);
227  if (ret < 0)
228  return ret;
229 
230  ret = accept(fd, NULL, NULL);
231  if (ret < 0)
232  return ff_neterrno();
233  if (ff_socket_nonblock(ret, 1) < 0)
234  av_log(NULL, AV_LOG_DEBUG, "ff_socket_nonblock failed\n");
235 
236  return ret;
237 }
238 
239 int ff_listen_bind(int fd, const struct sockaddr *addr,
240  socklen_t addrlen, int timeout, URLContext *h)
241 {
242  int ret;
243  if ((ret = ff_listen(fd, addr, addrlen)) < 0)
244  return ret;
245  if ((ret = ff_accept(fd, timeout, h)) < 0)
246  return ret;
247  closesocket(fd);
248  return ret;
249 }
250 
251 int ff_listen_connect(int fd, const struct sockaddr *addr,
252  socklen_t addrlen, int timeout, URLContext *h,
253  int will_try_next)
254 {
255  struct pollfd p = {fd, POLLOUT, 0};
256  int ret;
257  socklen_t optlen;
258 
259  if (ff_socket_nonblock(fd, 1) < 0)
260  av_log(NULL, AV_LOG_DEBUG, "ff_socket_nonblock failed\n");
261 
262  while ((ret = connect(fd, addr, addrlen))) {
263  ret = ff_neterrno();
264  switch (ret) {
265  case AVERROR(EINTR):
267  return AVERROR_EXIT;
268  continue;
269  case AVERROR(EINPROGRESS):
270  case AVERROR(EAGAIN):
271  ret = ff_poll_interrupt(&p, 1, timeout, &h->interrupt_callback);
272  if (ret < 0)
273  return ret;
274  optlen = sizeof(ret);
275  if (getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen))
276  ret = AVUNERROR(ff_neterrno());
277  if (ret != 0) {
278  char errbuf[100];
279  ret = AVERROR(ret);
280  av_strerror(ret, errbuf, sizeof(errbuf));
281  if (will_try_next)
283  "Connection to %s failed (%s), trying next address\n",
284  h->filename, errbuf);
285  else
286  av_log(h, AV_LOG_ERROR, "Connection to %s failed: %s\n",
287  h->filename, errbuf);
288  }
289  default:
290  return ret;
291  }
292  }
293  return ret;
294 }
295 
296 static int match_host_pattern(const char *pattern, const char *hostname)
297 {
298  int len_p, len_h;
299  if (!strcmp(pattern, "*"))
300  return 1;
301  // Skip a possible *. at the start of the pattern
302  if (pattern[0] == '*')
303  pattern++;
304  if (pattern[0] == '.')
305  pattern++;
306  len_p = strlen(pattern);
307  len_h = strlen(hostname);
308  if (len_p > len_h)
309  return 0;
310  // Simply check if the end of hostname is equal to 'pattern'
311  if (!strcmp(pattern, &hostname[len_h - len_p])) {
312  if (len_h == len_p)
313  return 1; // Exact match
314  if (hostname[len_h - len_p - 1] == '.')
315  return 1; // The matched substring is a domain and not just a substring of a domain
316  }
317  return 0;
318 }
319 
320 int ff_http_match_no_proxy(const char *no_proxy, const char *hostname)
321 {
322  char *buf, *start;
323  int ret = 0;
324  if (!no_proxy)
325  return 0;
326  if (!hostname)
327  return 0;
328  buf = av_strdup(no_proxy);
329  if (!buf)
330  return 0;
331  start = buf;
332  while (start) {
333  char *sep, *next = NULL;
334  start += strspn(start, " ,");
335  sep = start + strcspn(start, " ,");
336  if (*sep) {
337  next = sep + 1;
338  *sep = '\0';
339  }
340  if (match_host_pattern(start, hostname)) {
341  ret = 1;
342  break;
343  }
344  start = next;
345  }
346  av_free(buf);
347  return ret;
348 }
#define NULL
Definition: coverity.c:32
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
Memory handling functions.
static int match_host_pattern(const char *pattern, const char *hostname)
Definition: network.c:296
AVIOInterruptCB interrupt_callback
Definition: url.h:47
Convenience header that includes libavutil's core.
void ff_network_close(void)
Definition: network.c:115
int ff_tls_init(void)
Definition: network.c:30
static int ff_poll_interrupt(struct pollfd *p, nfds_t nfds, int timeout, AVIOInterruptCB *cb)
Definition: network.c:158
int av_usleep(unsigned usec)
Sleep for a period of time.
Definition: time.c:84
int ff_socket(int af, int type, int proto)
Definition: network.c:179
#define IN6_IS_ADDR_MULTICAST(a)
Definition: network.h:241
static double cb(void *priv, double x, double y)
Definition: vf_geq.c:112
int ff_listen_bind(int fd, const struct sockaddr *addr, socklen_t addrlen, int timeout, URLContext *h)
Bind to a file descriptor and poll for a connection.
Definition: network.c:239
int ff_network_init(void)
Definition: network.c:57
int ff_openssl_init(void)
Definition: tls_openssl.c:69
int ff_listen(int fd, const struct sockaddr *addr, socklen_t addrlen)
Bind to a file descriptor to an address without accepting connections.
Definition: network.c:203
void ff_gnutls_init(void)
Definition: tls_gnutls.c:56
int ff_listen_connect(int fd, const struct sockaddr *addr, socklen_t addrlen, int timeout, URLContext *h, int will_try_next)
Connect to a file descriptor and poll for result.
Definition: network.c:251
#define av_log(a,...)
Callback for checking whether to abort blocking functions.
Definition: avio.h:58
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
int ff_accept(int fd, int timeout, URLContext *h)
Poll for a single connection on the passed file descriptor.
Definition: network.c:221
const AVIOInterruptCB int_cb
Definition: ffmpeg.c:475
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
int ff_is_multicast_address(struct sockaddr *addr)
Definition: network.c:144
int ff_network_wait_fd_timeout(int fd, int write, int64_t timeout, AVIOInterruptCB *int_cb)
This works similarly to ff_network_wait_fd, but waits up to 'timeout' microseconds Uses ff_network_wa...
Definition: network.c:77
#define FFMIN(a, b)
Definition: common.h:96
#define ff_neterrno()
Definition: network.h:65
int ff_network_sleep_interruptible(int64_t timeout, AVIOInterruptCB *int_cb)
Waits for up to 'timeout' microseconds.
Definition: network.c:97
int ff_http_match_no_proxy(const char *no_proxy, const char *hostname)
Definition: network.c:320
int ff_socket_nonblock(int socket, int enable)
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
Definition: error.h:56
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:251
int ff_check_interrupt(AVIOInterruptCB *cb)
Check if the user has requested to interrupt a blocking function associated with cb.
Definition: avio.c:664
void * buf
Definition: avisynth_c.h:690
Definition: url.h:38
GLint GLenum type
Definition: opengl_enc.c:105
#define IN_MULTICAST(a)
Definition: network.h:238
#define POLLING_TIME
Definition: network.h:246
void ff_gnutls_deinit(void)
Definition: tls_gnutls.c:67
int64_t av_gettime_relative(void)
Get the current time in microseconds since some unspecified starting point.
Definition: time.c:56
int av_strerror(int errnum, char *errbuf, size_t errbuf_size)
Put a description of the AVERROR code errnum in errbuf.
Definition: error.c:105
int
common internal api header.
char * filename
specified URL
Definition: url.h:42
#define AVUNERROR(e)
Definition: error.h:44
#define av_free(p)
void ff_tls_deinit(void)
Definition: network.c:45
int ff_network_wait_fd(int fd, int write)
Definition: network.c:68
void INT64 start
Definition: avisynth_c.h:690
void ff_openssl_deinit(void)
Definition: tls_openssl.c:99
unbuffered private I/O API
#define MAKEWORD(a, b)
Definition: windows2linux.h:56