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_OPENSSL_PROTOCOL
33  int ret;
34  if ((ret = ff_openssl_init()) < 0)
35  return ret;
36 #endif
37 #if CONFIG_TLS_GNUTLS_PROTOCOL
39 #endif
40  return 0;
41 }
42 
43 void ff_tls_deinit(void)
44 {
45 #if CONFIG_TLS_OPENSSL_PROTOCOL
47 #endif
48 #if CONFIG_TLS_GNUTLS_PROTOCOL
50 #endif
51 }
52 
54 
55 int ff_network_init(void)
56 {
57 #if HAVE_WINSOCK2_H
58  WSADATA wsaData;
59 #endif
60 
62  av_log(NULL, AV_LOG_WARNING, "Using network protocols without global "
63  "network initialization. Please use "
64  "avformat_network_init(), this will "
65  "become mandatory later.\n");
66 #if HAVE_WINSOCK2_H
67  if (WSAStartup(MAKEWORD(1,1), &wsaData))
68  return 0;
69 #endif
70  return 1;
71 }
72 
73 int ff_network_wait_fd(int fd, int write)
74 {
75  int ev = write ? POLLOUT : POLLIN;
76  struct pollfd p = { .fd = fd, .events = ev, .revents = 0 };
77  int ret;
78  ret = poll(&p, 1, 100);
79  return ret < 0 ? ff_neterrno() : p.revents & (ev | POLLERR | POLLHUP) ? 0 : AVERROR(EAGAIN);
80 }
81 
82 int ff_network_wait_fd_timeout(int fd, int write, int64_t timeout, AVIOInterruptCB *int_cb)
83 {
84  int ret;
85  int64_t wait_start = 0;
86 
87  while (1) {
88  if (ff_check_interrupt(int_cb))
89  return AVERROR_EXIT;
90  ret = ff_network_wait_fd(fd, write);
91  if (ret != AVERROR(EAGAIN))
92  return ret;
93  if (timeout > 0) {
94  if (!wait_start)
95  wait_start = av_gettime_relative();
96  else if (av_gettime_relative() - wait_start > timeout)
97  return AVERROR(ETIMEDOUT);
98  }
99  }
100 }
101 
103 {
104 #if HAVE_WINSOCK2_H
105  WSACleanup();
106 #endif
107 }
108 
109 #if HAVE_WINSOCK2_H
110 int ff_neterrno(void)
111 {
112  int err = WSAGetLastError();
113  switch (err) {
114  case WSAEWOULDBLOCK:
115  return AVERROR(EAGAIN);
116  case WSAEINTR:
117  return AVERROR(EINTR);
118  case WSAEPROTONOSUPPORT:
119  return AVERROR(EPROTONOSUPPORT);
120  case WSAETIMEDOUT:
121  return AVERROR(ETIMEDOUT);
122  case WSAECONNREFUSED:
123  return AVERROR(ECONNREFUSED);
124  case WSAEINPROGRESS:
125  return AVERROR(EINPROGRESS);
126  }
127  return -err;
128 }
129 #endif
130 
131 int ff_is_multicast_address(struct sockaddr *addr)
132 {
133  if (addr->sa_family == AF_INET) {
134  return IN_MULTICAST(ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr));
135  }
136 #if HAVE_STRUCT_SOCKADDR_IN6
137  if (addr->sa_family == AF_INET6) {
138  return IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)addr)->sin6_addr);
139  }
140 #endif
141 
142  return 0;
143 }
144 
145 static int ff_poll_interrupt(struct pollfd *p, nfds_t nfds, int timeout,
147 {
148  int runs = timeout / POLLING_TIME;
149  int ret = 0;
150 
151  do {
152  if (ff_check_interrupt(cb))
153  return AVERROR_EXIT;
154  ret = poll(p, nfds, POLLING_TIME);
155  if (ret != 0)
156  break;
157  } while (timeout <= 0 || runs-- > 0);
158 
159  if (!ret)
160  return AVERROR(ETIMEDOUT);
161  if (ret < 0)
162  return AVERROR(errno);
163  return ret;
164 }
165 
166 int ff_socket(int af, int type, int proto)
167 {
168  int fd;
169 
170 #ifdef SOCK_CLOEXEC
171  fd = socket(af, type | SOCK_CLOEXEC, proto);
172  if (fd == -1 && errno == EINVAL)
173 #endif
174  {
175  fd = socket(af, type, proto);
176 #if HAVE_FCNTL
177  if (fd != -1) {
178  if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
179  av_log(NULL, AV_LOG_DEBUG, "Failed to set close on exec\n");
180  }
181 #endif
182  }
183 #ifdef SO_NOSIGPIPE
184  if (fd != -1)
185  setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &(int){1}, sizeof(int));
186 #endif
187  return fd;
188 }
189 
190 int ff_listen_bind(int fd, const struct sockaddr *addr,
191  socklen_t addrlen, int timeout, URLContext *h)
192 {
193  int ret;
194  int reuse = 1;
195  struct pollfd lp = { fd, POLLIN, 0 };
196  if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse))) {
197  av_log(NULL, AV_LOG_WARNING, "setsockopt(SO_REUSEADDR) failed\n");
198  }
199  ret = bind(fd, addr, addrlen);
200  if (ret)
201  return ff_neterrno();
202 
203  ret = listen(fd, 1);
204  if (ret)
205  return ff_neterrno();
206 
207  ret = ff_poll_interrupt(&lp, 1, timeout, &h->interrupt_callback);
208  if (ret < 0)
209  return ret;
210 
211  ret = accept(fd, NULL, NULL);
212  if (ret < 0)
213  return ff_neterrno();
214 
215  closesocket(fd);
216 
217  if (ff_socket_nonblock(ret, 1) < 0)
218  av_log(NULL, AV_LOG_DEBUG, "ff_socket_nonblock failed\n");
219 
220  return ret;
221 }
222 
223 int ff_listen_connect(int fd, const struct sockaddr *addr,
224  socklen_t addrlen, int timeout, URLContext *h,
225  int will_try_next)
226 {
227  struct pollfd p = {fd, POLLOUT, 0};
228  int ret;
229  socklen_t optlen;
230 
231  if (ff_socket_nonblock(fd, 1) < 0)
232  av_log(NULL, AV_LOG_DEBUG, "ff_socket_nonblock failed\n");
233 
234  while ((ret = connect(fd, addr, addrlen))) {
235  ret = ff_neterrno();
236  switch (ret) {
237  case AVERROR(EINTR):
239  return AVERROR_EXIT;
240  continue;
241  case AVERROR(EINPROGRESS):
242  case AVERROR(EAGAIN):
243  ret = ff_poll_interrupt(&p, 1, timeout, &h->interrupt_callback);
244  if (ret < 0)
245  return ret;
246  optlen = sizeof(ret);
247  if (getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen))
248  ret = AVUNERROR(ff_neterrno());
249  if (ret != 0) {
250  char errbuf[100];
251  ret = AVERROR(ret);
252  av_strerror(ret, errbuf, sizeof(errbuf));
253  if (will_try_next)
255  "Connection to %s failed (%s), trying next address\n",
256  h->filename, errbuf);
257  else
258  av_log(h, AV_LOG_ERROR, "Connection to %s failed: %s\n",
259  h->filename, errbuf);
260  }
261  default:
262  return ret;
263  }
264  }
265  return ret;
266 }
267 
268 static int match_host_pattern(const char *pattern, const char *hostname)
269 {
270  int len_p, len_h;
271  if (!strcmp(pattern, "*"))
272  return 1;
273  // Skip a possible *. at the start of the pattern
274  if (pattern[0] == '*')
275  pattern++;
276  if (pattern[0] == '.')
277  pattern++;
278  len_p = strlen(pattern);
279  len_h = strlen(hostname);
280  if (len_p > len_h)
281  return 0;
282  // Simply check if the end of hostname is equal to 'pattern'
283  if (!strcmp(pattern, &hostname[len_h - len_p])) {
284  if (len_h == len_p)
285  return 1; // Exact match
286  if (hostname[len_h - len_p - 1] == '.')
287  return 1; // The matched substring is a domain and not just a substring of a domain
288  }
289  return 0;
290 }
291 
292 int ff_http_match_no_proxy(const char *no_proxy, const char *hostname)
293 {
294  char *buf, *start;
295  int ret = 0;
296  if (!no_proxy)
297  return 0;
298  if (!hostname)
299  return 0;
300  buf = av_strdup(no_proxy);
301  if (!buf)
302  return 0;
303  start = buf;
304  while (start) {
305  char *sep, *next = NULL;
306  start += strspn(start, " ,");
307  sep = start + strcspn(start, " ,");
308  if (*sep) {
309  next = sep + 1;
310  *sep = '\0';
311  }
312  if (match_host_pattern(start, hostname)) {
313  ret = 1;
314  break;
315  }
316  start = next;
317  }
318  av_free(buf);
319  return ret;
320 }
#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:268
AVIOInterruptCB interrupt_callback
Definition: url.h:48
external API header
void ff_network_close(void)
Definition: network.c:102
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:145
int ff_socket(int af, int type, int proto)
Definition: network.c:166
int ff_network_inited_globally
Definition: network.c:53
#define IN6_IS_ADDR_MULTICAST(a)
Definition: network.h:234
static double cb(void *priv, double x, double y)
Definition: vf_geq.c:96
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:190
int ff_network_init(void)
Definition: network.c:55
int ff_openssl_init(void)
Definition: tls_openssl.c:66
void ff_gnutls_init(void)
Definition: tls_gnutls.c:52
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:223
#define av_log(a,...)
Callback for checking whether to abort blocking functions.
Definition: avio.h:50
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
const AVIOInterruptCB int_cb
Definition: ffmpeg.c:424
#define closesocket
Definition: ffserver.c:28
#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:131
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:82
ret
Definition: avfilter.c:974
#define ff_neterrno()
Definition: network.h:64
int ff_http_match_no_proxy(const char *no_proxy, const char *hostname)
Definition: network.c:292
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
int ff_check_interrupt(AVIOInterruptCB *cb)
Check if the user has requested to interrup a blocking function associated with cb.
Definition: avio.c:541
char * av_strdup(const char *s)
Duplicate the string s.
Definition: mem.c:265
void * buf
Definition: avisynth_c.h:553
Definition: url.h:39
GLint GLenum type
Definition: opengl_enc.c:105
#define IN_MULTICAST(a)
Definition: network.h:231
#define POLLING_TIME
Definition: network.h:239
void ff_gnutls_deinit(void)
Definition: tls_gnutls.c:63
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:68
common internal api header.
char * filename
specified URL
Definition: url.h:43
#define AVUNERROR(e)
Definition: error.h:44
#define av_free(p)
void ff_tls_deinit(void)
Definition: network.c:43
int ff_network_wait_fd(int fd, int write)
Definition: network.c:73
void INT64 start
Definition: avisynth_c.h:553
void ff_openssl_deinit(void)
Definition: tls_openssl.c:96
unbuffered private I/O API
#define MAKEWORD(a, b)
Definition: windows2linux.h:56