FFmpeg
network.h
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 #ifndef AVFORMAT_NETWORK_H
22 #define AVFORMAT_NETWORK_H
23 
24 #include <errno.h>
25 #include <stdint.h>
26 
27 #include "config.h"
28 #include "libavutil/error.h"
29 #include "os_support.h"
30 #include "avio.h"
31 #include "url.h"
32 
33 #if HAVE_UNISTD_H
34 #include <unistd.h>
35 #endif
36 
37 #if HAVE_WINSOCK2_H
38 #include <winsock2.h>
39 #include <ws2tcpip.h>
40 
41 #ifndef EPROTONOSUPPORT
42 #define EPROTONOSUPPORT WSAEPROTONOSUPPORT
43 #endif
44 #ifndef ETIMEDOUT
45 #define ETIMEDOUT WSAETIMEDOUT
46 #endif
47 #ifndef ECONNREFUSED
48 #define ECONNREFUSED WSAECONNREFUSED
49 #endif
50 #ifndef EINPROGRESS
51 #define EINPROGRESS WSAEINPROGRESS
52 #endif
53 #ifndef ENOTCONN
54 #define ENOTCONN WSAENOTCONN
55 #endif
56 
57 #define getsockopt(a, b, c, d, e) getsockopt(a, b, c, (char*) d, e)
58 #define setsockopt(a, b, c, d, e) setsockopt(a, b, c, (const char*) d, e)
59 
60 int ff_neterrno(void);
61 #else
62 #include <sys/types.h>
63 #include <sys/socket.h>
64 #include <netinet/in.h>
65 #include <netinet/tcp.h>
66 #include <netdb.h>
67 
68 #define ff_neterrno() AVERROR(errno)
69 #endif /* HAVE_WINSOCK2_H */
70 
71 #if HAVE_ARPA_INET_H
72 #include <arpa/inet.h>
73 #endif
74 
75 #if HAVE_POLL_H
76 #include <poll.h>
77 #endif
78 
79 int ff_socket_nonblock(int socket, int enable);
80 
81 int ff_network_init(void);
82 void ff_network_close(void);
83 
84 int ff_tls_init(void);
85 void ff_tls_deinit(void);
86 
87 int ff_network_wait_fd(int fd, int write);
88 
89 /**
90  * This works similarly to ff_network_wait_fd, but waits up to 'timeout' microseconds
91  * Uses ff_network_wait_fd in a loop
92  *
93  * @param fd Socket descriptor
94  * @param write Set 1 to wait for socket able to be read, 0 to be written
95  * @param timeout Timeout interval, in microseconds. Actual precision is 100000 mcs, due to ff_network_wait_fd usage
96  * @param int_cb Interrupt callback, is checked before each ff_network_wait_fd call
97  * @return 0 if data can be read/written, AVERROR(ETIMEDOUT) if timeout expired, or negative error code
98  */
99 int ff_network_wait_fd_timeout(int fd, int write, int64_t timeout, AVIOInterruptCB *int_cb);
100 
101 /**
102  * Waits for up to 'timeout' microseconds. If the usert's int_cb is set and
103  * triggered, return before that.
104  * @param timeout Timeout in microseconds. Maybe have lower actual precision.
105  * @param int_cb Interrupt callback, is checked regularly.
106  * @return AVERROR(ETIMEDOUT) if timeout expirted, AVERROR_EXIT if interrupted by int_cb
107  */
109 
110 #if !HAVE_STRUCT_SOCKADDR_STORAGE
112 #if HAVE_STRUCT_SOCKADDR_SA_LEN
113  uint8_t ss_len;
114  uint8_t ss_family;
115 #else
116  uint16_t ss_family;
117 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
118  char ss_pad1[6];
119  int64_t ss_align;
120  char ss_pad2[112];
121 };
122 #endif /* !HAVE_STRUCT_SOCKADDR_STORAGE */
123 
124 typedef union sockaddr_union {
126  struct sockaddr_in in;
127 #if HAVE_STRUCT_SOCKADDR_IN6
128  struct sockaddr_in6 in6;
129 #endif
131 
132 #ifndef MSG_NOSIGNAL
133 #define MSG_NOSIGNAL 0
134 #endif
135 
136 #if !HAVE_STRUCT_ADDRINFO
137 struct addrinfo {
138  int ai_flags;
143  struct sockaddr *ai_addr;
145  struct addrinfo *ai_next;
146 };
147 #endif /* !HAVE_STRUCT_ADDRINFO */
148 
149 /* getaddrinfo constants */
150 #ifndef EAI_AGAIN
151 #define EAI_AGAIN 2
152 #endif
153 #ifndef EAI_BADFLAGS
154 #define EAI_BADFLAGS 3
155 #endif
156 #ifndef EAI_FAIL
157 #define EAI_FAIL 4
158 #endif
159 #ifndef EAI_FAMILY
160 #define EAI_FAMILY 5
161 #endif
162 #ifndef EAI_MEMORY
163 #define EAI_MEMORY 6
164 #endif
165 #ifndef EAI_NODATA
166 #define EAI_NODATA 7
167 #endif
168 #ifndef EAI_NONAME
169 #define EAI_NONAME 8
170 #endif
171 #ifndef EAI_SERVICE
172 #define EAI_SERVICE 9
173 #endif
174 #ifndef EAI_SOCKTYPE
175 #define EAI_SOCKTYPE 10
176 #endif
177 
178 #ifndef AI_PASSIVE
179 #define AI_PASSIVE 1
180 #endif
181 
182 #ifndef AI_CANONNAME
183 #define AI_CANONNAME 2
184 #endif
185 
186 #ifndef AI_NUMERICHOST
187 #define AI_NUMERICHOST 4
188 #endif
189 
190 #ifndef NI_NOFQDN
191 #define NI_NOFQDN 1
192 #endif
193 
194 #ifndef NI_NUMERICHOST
195 #define NI_NUMERICHOST 2
196 #endif
197 
198 #ifndef NI_NAMERQD
199 #define NI_NAMERQD 4
200 #endif
201 
202 #ifndef NI_NUMERICSERV
203 #define NI_NUMERICSERV 8
204 #endif
205 
206 #ifndef NI_DGRAM
207 #define NI_DGRAM 16
208 #endif
209 
210 #if !HAVE_GETADDRINFO
211 int ff_getaddrinfo(const char *node, const char *service,
212  const struct addrinfo *hints, struct addrinfo **res);
213 void ff_freeaddrinfo(struct addrinfo *res);
214 int ff_getnameinfo(const struct sockaddr *sa, int salen,
215  char *host, int hostlen,
216  char *serv, int servlen, int flags);
217 #define getaddrinfo ff_getaddrinfo
218 #define freeaddrinfo ff_freeaddrinfo
219 #define getnameinfo ff_getnameinfo
220 #endif /* !HAVE_GETADDRINFO */
221 
222 #if !HAVE_GETADDRINFO || HAVE_WINSOCK2_H
223 const char *ff_gai_strerror(int ecode);
224 #undef gai_strerror
225 #define gai_strerror ff_gai_strerror
226 #endif /* !HAVE_GETADDRINFO || HAVE_WINSOCK2_H */
227 
228 #ifndef INADDR_LOOPBACK
229 #define INADDR_LOOPBACK 0x7f000001
230 #endif
231 
232 #ifndef INET_ADDRSTRLEN
233 #define INET_ADDRSTRLEN 16
234 #endif
235 
236 #ifndef INET6_ADDRSTRLEN
237 #define INET6_ADDRSTRLEN INET_ADDRSTRLEN
238 #endif
239 
240 #ifndef IN_MULTICAST
241 #define IN_MULTICAST(a) ((((uint32_t)(a)) & 0xf0000000) == 0xe0000000)
242 #endif
243 #ifndef IN6_IS_ADDR_MULTICAST
244 #define IN6_IS_ADDR_MULTICAST(a) (((uint8_t *) (a))[0] == 0xff)
245 #endif
246 
247 int ff_is_multicast_address(struct sockaddr *addr);
248 
249 #define POLLING_TIME 100 /// Time in milliseconds between interrupt check
250 
251 /**
252  * Bind to a file descriptor and poll for a connection.
253  *
254  * @param fd First argument of bind().
255  * @param addr Second argument of bind().
256  * @param addrlen Third argument of bind().
257  * @param timeout Polling timeout in milliseconds.
258  * @param h URLContext providing interrupt check
259  * callback and logging context.
260  * @return A non-blocking file descriptor on success
261  * or an AVERROR on failure.
262  */
263 int ff_listen_bind(int fd, const struct sockaddr *addr,
264  socklen_t addrlen, int timeout,
265  URLContext *h);
266 
267 /**
268  * Bind to a file descriptor to an address without accepting connections.
269  * @param fd First argument of bind().
270  * @param addr Second argument of bind().
271  * @param addrlen Third argument of bind().
272  * @return 0 on success or an AVERROR on failure.
273  */
274 int ff_listen(int fd, const struct sockaddr *addr, socklen_t addrlen,
275  void *logctx);
276 
277 /**
278  * Poll for a single connection on the passed file descriptor.
279  * @param fd The listening socket file descriptor.
280  * @param timeout Polling timeout in milliseconds.
281  * @param h URLContext providing interrupt check
282  * callback and logging context.
283  * @return A non-blocking file descriptor on success
284  * or an AVERROR on failure.
285  */
286 int ff_accept(int fd, int timeout, URLContext *h);
287 
288 /**
289  * Connect to a file descriptor and poll for result.
290  *
291  * @param fd First argument of connect(),
292  * will be set as non-blocking.
293  * @param addr Second argument of connect().
294  * @param addrlen Third argument of connect().
295  * @param timeout Polling timeout in milliseconds.
296  * @param h URLContext providing interrupt check
297  * callback and logging context.
298  * @param will_try_next Whether the caller will try to connect to another
299  * address for the same host name, affecting the form of
300  * logged errors.
301  * @return 0 on success, AVERROR on failure.
302  */
303 int ff_listen_connect(int fd, const struct sockaddr *addr,
304  socklen_t addrlen, int timeout,
305  URLContext *h, int will_try_next);
306 
307 int ff_http_match_no_proxy(const char *no_proxy, const char *hostname);
308 
309 int ff_socket(int domain, int type, int protocol, void *logctx);
310 
311 void ff_log_net_error(void *ctx, int level, const char* prefix);
312 
313 /**
314  * Connect to any of the given addrinfo addresses, with multiple attempts
315  * running in parallel.
316  *
317  * @param addrs The list of addresses to try to connect to.
318  * This list will be mutated internally, but the list head
319  * will remain as such, so this doesn't affect the caller
320  * freeing the list afterwards.
321  * @param timeout_ms_per_address The number of milliseconds to wait for each
322  * connection attempt. Since multiple addresses are tried,
323  * some of them in parallel, the total run time will at most
324  * be timeout_ms_per_address*ceil(nb_addrs/parallel) +
325  * (parallel - 1) * NEXT_ATTEMPT_DELAY_MS.
326  * @param parallel The maximum number of connections to attempt in parallel.
327  * This is limited to an internal maximum capacity.
328  * @param h URLContext providing interrupt check
329  * callback and logging context.
330  * @param fd If successful, the connected socket is returned here.
331  * @param customize_fd Function that will be called for each socket created,
332  * to allow the caller to set socket options before calling
333  * connect() on it, may be NULL.
334  * @param customize_ctx Context parameter passed to customize_fd.
335  * @return 0 on success, AVERROR on failure.
336  */
337 int ff_connect_parallel(struct addrinfo *addrs, int timeout_ms_per_address,
338  int parallel, URLContext *h, int *fd,
339  int (*customize_fd)(void *, int, int), void *customize_ctx);
340 
341 #endif /* AVFORMAT_NETWORK_H */
ff_listen
int ff_listen(int fd, const struct sockaddr *addr, socklen_t addrlen, void *logctx)
Bind to a file descriptor to an address without accepting connections.
Definition: network.c:210
level
uint8_t level
Definition: svq3.c:205
ff_getaddrinfo
int ff_getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res)
ff_network_sleep_interruptible
int ff_network_sleep_interruptible(int64_t timeout, AVIOInterruptCB *int_cb)
Waits for up to 'timeout' microseconds.
Definition: network.c:98
sockaddr_union::in
struct sockaddr_in in
Definition: network.h:126
sockaddr_storage::ss_pad1
char ss_pad1[6]
Definition: network.h:118
ff_connect_parallel
int ff_connect_parallel(struct addrinfo *addrs, int timeout_ms_per_address, int parallel, URLContext *h, int *fd, int(*customize_fd)(void *, int, int), void *customize_ctx)
Connect to any of the given addrinfo addresses, with multiple attempts running in parallel.
Definition: network.c:409
ff_http_match_no_proxy
int ff_http_match_no_proxy(const char *no_proxy, const char *hostname)
Definition: network.c:557
os_support.h
sockaddr_storage
Definition: network.h:111
AVIOInterruptCB
Callback for checking whether to abort blocking functions.
Definition: avio.h:59
ff_network_wait_fd
int ff_network_wait_fd(int fd, int write)
Definition: network.c:69
ff_socket
int ff_socket(int domain, int type, int protocol, void *logctx)
Definition: network.c:183
ff_getnameinfo
int ff_getnameinfo(const struct sockaddr *sa, int salen, char *host, int hostlen, char *serv, int servlen, int flags)
ff_is_multicast_address
int ff_is_multicast_address(struct sockaddr *addr)
Definition: network.c:145
sockaddr_storage::ss_family
uint16_t ss_family
Definition: network.h:116
ff_network_wait_fd_timeout
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:78
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
ff_freeaddrinfo
void ff_freeaddrinfo(struct addrinfo *res)
customize_fd
static int customize_fd(void *ctx, int fd, int family)
Definition: tcp.c:77
ctx
AVFormatContext * ctx
Definition: movenc.c:49
ff_network_close
void ff_network_close(void)
Definition: network.c:116
ff_tls_init
int ff_tls_init(void)
Definition: network.c:31
sockaddr_union::storage
struct sockaddr_storage storage
Definition: network.h:125
ff_gai_strerror
const char * ff_gai_strerror(int ecode)
addrinfo::ai_canonname
char * ai_canonname
Definition: network.h:144
ff_neterrno
#define ff_neterrno()
Definition: network.h:68
addrinfo::ai_addr
struct sockaddr * ai_addr
Definition: network.h:143
ff_tls_deinit
void ff_tls_deinit(void)
Definition: network.c:46
error.h
ff_log_net_error
void ff_log_net_error(void *ctx, int level, const char *prefix)
Definition: network.c:587
addrinfo::ai_family
int ai_family
Definition: network.h:139
sockaddr_storage::ss_align
int64_t ss_align
Definition: network.h:119
avio.h
addrinfo::ai_protocol
int ai_protocol
Definition: network.h:141
ff_socket_nonblock
int ff_socket_nonblock(int socket, int enable)
addrinfo::ai_next
struct addrinfo * ai_next
Definition: network.h:145
addrinfo::ai_addrlen
int ai_addrlen
Definition: network.h:142
URLContext
Definition: url.h:35
ff_listen_bind
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:246
ff_listen_connect
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:258
url.h
int_cb
const AVIOInterruptCB int_cb
Definition: ffmpeg.c:306
addrinfo::ai_socktype
int ai_socktype
Definition: network.h:140
ff_accept
int ff_accept(int fd, int timeout, URLContext *h)
Poll for a single connection on the passed file descriptor.
Definition: network.c:228
ff_network_init
int ff_network_init(void)
Definition: network.c:58
addrinfo::ai_flags
int ai_flags
Definition: network.h:138
sockaddr_union
Definition: network.h:124
sockaddr_storage::ss_pad2
char ss_pad2[112]
Definition: network.h:120
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:474
h
h
Definition: vp9dsp_template.c:2038
addrinfo
Definition: network.h:137