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  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):
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)
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 }
#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:299
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:242
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:206
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:254
#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:224
const AVIOInterruptCB int_cb
Definition: ffmpeg.c:481
#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
AVFormatContext * ctx
Definition: movenc.c:48
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:323
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
uint8_t level
Definition: svq3.c:207
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
void ff_log_net_error(void *ctx, int level, const char *prefix)
Definition: network.c:353