FFmpeg
Loading...
Searching...
No Matches
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 <stdatomic.h>
26#include <stdint.h>
27
28#include "config.h"
29#include "libavutil/error.h"
30#include "os_support.h"
31#include "avio.h"
32#include "url.h"
33
34#if HAVE_UNISTD_H
35#include <unistd.h>
36#endif
37
38#if HAVE_WINSOCK2_H
39#include <winsock2.h>
40#include <ws2tcpip.h>
41
42#ifndef EPROTONOSUPPORT
43#define EPROTONOSUPPORT WSAEPROTONOSUPPORT
44#endif
45#ifndef ETIMEDOUT
46#define ETIMEDOUT WSAETIMEDOUT
47#endif
48#ifndef ECONNREFUSED
49#define ECONNREFUSED WSAECONNREFUSED
50#endif
51#ifndef EINPROGRESS
52#define EINPROGRESS WSAEINPROGRESS
53#endif
54#ifndef ENOTCONN
55#define ENOTCONN WSAENOTCONN
56#endif
57
58#define getsockopt(a, b, c, d, e) getsockopt(a, b, c, (char*) d, e)
59#define setsockopt(a, b, c, d, e) setsockopt(a, b, c, (const char*) d, e)
60
61int ff_neterrno(void);
62#else
63#include <sys/types.h>
64#include <sys/socket.h>
65#include <netinet/in.h>
66#include <netinet/tcp.h>
67#include <netdb.h>
68
69#define ff_neterrno() AVERROR(errno)
70#endif /* HAVE_WINSOCK2_H */
71
72#if HAVE_ARPA_INET_H
73#include <arpa/inet.h>
74#endif
75
76#if HAVE_POLL_H
77#include <poll.h>
78#endif
79
80int ff_socket_nonblock(int socket, int enable);
81
82int ff_network_init(void);
83void ff_network_close(void);
84
85int ff_tls_init(void);
86void ff_tls_deinit(void);
87
88int ff_network_wait_fd(int fd, int write);
89
90/**
91 * This works similarly to ff_network_wait_fd, but waits up to 'timeout' microseconds
92 * Uses ff_network_wait_fd in a loop
93 *
94 * @param fd Socket descriptor
95 * @param write Set 1 to wait for socket able to be read, 0 to be written
96 * @param timeout Timeout interval, in microseconds. Actual precision is 100000 mcs, due to ff_network_wait_fd usage
97 * @param int_cb Interrupt callback, is checked before each ff_network_wait_fd call
98 * @return 0 if data can be read/written, AVERROR(ETIMEDOUT) if timeout expired, or negative error code
99 */
100int ff_network_wait_fd_timeout(int fd, int write, int64_t timeout, AVIOInterruptCB *int_cb);
101
102/**
103 * Waits for up to 'timeout' microseconds. If the usert's int_cb is set and
104 * triggered, return before that.
105 * @param timeout Timeout in microseconds. Maybe have lower actual precision.
106 * @param int_cb Interrupt callback, is checked regularly.
107 * @return AVERROR(ETIMEDOUT) if timeout expirted, AVERROR_EXIT if interrupted by int_cb
108 */
110
111/**
112 * A channel for waking a thread that is blocked in
113 * ff_network_wait_fd_wakeup(), from another thread.
114 *
115 * Usage: the waiting thread calls ff_network_wait_fd_wakeup(); another
116 * thread calls ff_network_wakeup_signal() to end that wait; once the
117 * waiting thread has been joined, ff_network_wakeup_close() is called.
118 *
119 * rfd is pollable together with sockets on every platform. It is an
120 * eventfd, the read end of a pipe, or a loopback datagram socket on
121 * Windows, where only sockets can be waited on together with sockets.
122 * wfd is the end ff_network_wakeup_signal() writes to; it equals rfd
123 * unless a pipe is in use. On systems other than Windows both fds are
124 * close-on-exec.
125 */
131
132/**
133 * Create a wakeup channel. On Windows ff_network_init() must have been
134 * called first.
135 *
136 * @return 0 on success, a negative AVERROR code on failure
137 */
139
140/**
141 * Wake the thread blocked in ff_network_wait_fd_wakeup() on this channel,
142 * or the next one that waits on it: the signal is not consumed, so a
143 * signal sent before the wait starts is not lost. Only the first call has
144 * an effect; the function may be called any number of times, from any
145 * thread.
146 *
147 * @return 0 on success, a negative AVERROR code if the first call failed
148 * to write to the channel
149 */
151
152/**
153 * Close the channel. Call it after the waiting thread has been joined and
154 * no thread can signal any more.
155 */
157
158/**
159 * Like ff_network_wait_fd(), but also returns as soon as the wakeup
160 * channel is signaled, and with a caller chosen timeout.
161 *
162 * @param fd Socket descriptor
163 * @param write Set 1 to wait for the socket to be writable, 0 for readable
164 * @param timeout_ms Timeout in milliseconds, or -1 to wait indefinitely
165 * @param w Wakeup channel to watch, must have been opened
166 * @return 0 if fd is ready, AVERROR(EAGAIN) if the timeout expired,
167 * AVERROR_EXIT if w was signaled (also when fd is ready),
168 * or a negative error code
169 */
170int ff_network_wait_fd_wakeup(int fd, int write, int timeout_ms,
171 const FFNetWakeup *w);
172
173#if !HAVE_STRUCT_SOCKADDR_STORAGE
175#if HAVE_STRUCT_SOCKADDR_SA_LEN
176 uint8_t ss_len;
177 uint8_t ss_family;
178#else
179 uint16_t ss_family;
180#endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
181 char ss_pad1[6];
183 char ss_pad2[112];
184};
185#endif /* !HAVE_STRUCT_SOCKADDR_STORAGE */
186
187typedef union sockaddr_union {
189 struct sockaddr_in in;
190#if HAVE_STRUCT_SOCKADDR_IN6
191 struct sockaddr_in6 in6;
192#endif
194
195#ifndef MSG_NOSIGNAL
196#define MSG_NOSIGNAL 0
197#endif
198
199#if !HAVE_STRUCT_ADDRINFO
210#endif /* !HAVE_STRUCT_ADDRINFO */
211
212/* getaddrinfo constants */
213#ifndef EAI_AGAIN
214#define EAI_AGAIN 2
215#endif
216#ifndef EAI_BADFLAGS
217#define EAI_BADFLAGS 3
218#endif
219#ifndef EAI_FAIL
220#define EAI_FAIL 4
221#endif
222#ifndef EAI_FAMILY
223#define EAI_FAMILY 5
224#endif
225#ifndef EAI_MEMORY
226#define EAI_MEMORY 6
227#endif
228#ifndef EAI_NODATA
229#define EAI_NODATA 7
230#endif
231#ifndef EAI_NONAME
232#define EAI_NONAME 8
233#endif
234#ifndef EAI_SERVICE
235#define EAI_SERVICE 9
236#endif
237#ifndef EAI_SOCKTYPE
238#define EAI_SOCKTYPE 10
239#endif
240
241#ifndef AI_PASSIVE
242#define AI_PASSIVE 1
243#endif
244
245#ifndef AI_CANONNAME
246#define AI_CANONNAME 2
247#endif
248
249#ifndef AI_NUMERICHOST
250#define AI_NUMERICHOST 4
251#endif
252
253#ifndef NI_NOFQDN
254#define NI_NOFQDN 1
255#endif
256
257#ifndef NI_NUMERICHOST
258#define NI_NUMERICHOST 2
259#endif
260
261#ifndef NI_NAMERQD
262#define NI_NAMERQD 4
263#endif
264
265#ifndef NI_NUMERICSERV
266#define NI_NUMERICSERV 8
267#endif
268
269#ifndef NI_DGRAM
270#define NI_DGRAM 16
271#endif
272
273#if !HAVE_GETADDRINFO
274int ff_getaddrinfo(const char *node, const char *service,
275 const struct addrinfo *hints, struct addrinfo **res);
276void ff_freeaddrinfo(struct addrinfo *res);
277int ff_getnameinfo(const struct sockaddr *sa, int salen,
278 char *host, int hostlen,
279 char *serv, int servlen, int flags);
280#define getaddrinfo ff_getaddrinfo
281#define freeaddrinfo ff_freeaddrinfo
282#define getnameinfo ff_getnameinfo
283#endif /* !HAVE_GETADDRINFO */
284
285#if !HAVE_GETADDRINFO || HAVE_WINSOCK2_H
286const char *ff_gai_strerror(int ecode);
287#undef gai_strerror
288#define gai_strerror ff_gai_strerror
289#endif /* !HAVE_GETADDRINFO || HAVE_WINSOCK2_H */
290
291#ifndef INADDR_LOOPBACK
292#define INADDR_LOOPBACK 0x7f000001
293#endif
294
295#ifndef INET_ADDRSTRLEN
296#define INET_ADDRSTRLEN 16
297#endif
298
299#ifndef INET6_ADDRSTRLEN
300#define INET6_ADDRSTRLEN INET_ADDRSTRLEN
301#endif
302
303#ifndef IN_MULTICAST
304#define IN_MULTICAST(a) ((((uint32_t)(a)) & 0xf0000000) == 0xe0000000)
305#endif
306#ifndef IN6_IS_ADDR_MULTICAST
307#define IN6_IS_ADDR_MULTICAST(a) (((uint8_t *) (a))[0] == 0xff)
308#endif
309
310int ff_is_multicast_address(struct sockaddr *addr);
311
312#define POLLING_TIME 100 /// Time in milliseconds between interrupt check
313
314/**
315 * Bind to a file descriptor and poll for a connection.
316 *
317 * @param fd First argument of bind().
318 * @param addr Second argument of bind().
319 * @param addrlen Third argument of bind().
320 * @param timeout Polling timeout in milliseconds.
321 * @param h URLContext providing interrupt check
322 * callback and logging context.
323 * @return A non-blocking file descriptor on success
324 * or an AVERROR on failure.
325 */
326int ff_listen_bind(int fd, const struct sockaddr *addr,
327 socklen_t addrlen, int timeout,
328 URLContext *h);
329
330/**
331 * Bind to a file descriptor to an address without accepting connections.
332 * @param fd First argument of bind().
333 * @param addr Second argument of bind().
334 * @param addrlen Third argument of bind().
335 * @return 0 on success or an AVERROR on failure.
336 */
337int ff_listen(int fd, const struct sockaddr *addr, socklen_t addrlen,
338 void *logctx);
339
340/**
341 * Poll for a single connection on the passed file descriptor.
342 * @param fd The listening socket file descriptor.
343 * @param timeout Polling timeout in milliseconds.
344 * @param h URLContext providing interrupt check
345 * callback and logging context.
346 * @return A non-blocking file descriptor on success
347 * or an AVERROR on failure.
348 */
349int ff_accept(int fd, int timeout, URLContext *h);
350
351/**
352 * Connect to a file descriptor and poll for result.
353 *
354 * @param fd First argument of connect(),
355 * will be set as non-blocking.
356 * @param addr Second argument of connect().
357 * @param addrlen Third argument of connect().
358 * @param timeout Polling timeout in milliseconds.
359 * @param h URLContext providing interrupt check
360 * callback and logging context.
361 * @param will_try_next Whether the caller will try to connect to another
362 * address for the same host name, affecting the form of
363 * logged errors.
364 * @return 0 on success, AVERROR on failure.
365 */
366int ff_listen_connect(int fd, const struct sockaddr *addr,
367 socklen_t addrlen, int timeout,
368 URLContext *h, int will_try_next);
369
370int ff_http_match_no_proxy(const char *no_proxy, const char *hostname);
371
372int ff_socket(int domain, int type, int protocol, void *logctx);
373
374void ff_log_net_error(void *ctx, int level, const char* prefix);
375
376/**
377 * Connect to any of the given addrinfo addresses, with multiple attempts
378 * running in parallel.
379 *
380 * @param addrs The list of addresses to try to connect to.
381 * This list will be mutated internally, but the list head
382 * will remain as such, so this doesn't affect the caller
383 * freeing the list afterwards.
384 * @param timeout_ms_per_address The number of milliseconds to wait for each
385 * connection attempt. Since multiple addresses are tried,
386 * some of them in parallel, the total run time will at most
387 * be timeout_ms_per_address*ceil(nb_addrs/parallel) +
388 * (parallel - 1) * NEXT_ATTEMPT_DELAY_MS.
389 * @param parallel The maximum number of connections to attempt in parallel.
390 * This is limited to an internal maximum capacity.
391 * @param h URLContext providing interrupt check
392 * callback and logging context.
393 * @param fd If successful, the connected socket is returned here.
394 * @param customize_fd Function that will be called for each socket created,
395 * to allow the caller to set socket options before calling
396 * connect() on it, may be NULL.
397 * @param customize_ctx Context parameter passed to customize_fd.
398 * @return 0 on success, AVERROR on failure.
399 */
400int ff_connect_parallel(struct addrinfo *addrs, int timeout_ms_per_address,
401 int parallel, URLContext *h, int *fd,
402 int (*customize_fd)(void *, int, int), void *customize_ctx);
403
404void ff_udp_get_last_recv_addr(URLContext *h, struct sockaddr_storage *addr, socklen_t *addr_len);
405int ff_udp_set_remote_addr(URLContext *h, const struct sockaddr *dest_addr, socklen_t dest_addr_len, int do_connect);
406
407#endif /* AVFORMAT_NETWORK_H */
static AVFormatContext * ctx
Buffered I/O operations.
#define flags(name, subs,...)
Definition cbs_h264.c:74
long long int64_t
Definition coverity.c:34
error code definitions
const AVIOInterruptCB int_cb
Definition ffmpeg.c:322
enum MPFType type
Definition jpegmpfenc.c:55
uint8_t w
Definition llvidencdsp.c:39
char * host
Definition mscl.c:249
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:531
int ff_is_multicast_address(struct sockaddr *addr)
Definition network.c:268
int ff_socket_nonblock(int socket, int enable)
int ff_socket(int domain, int type, int protocol, void *logctx)
Definition network.c:306
int ff_network_wakeup_open(FFNetWakeup *w)
Create a wakeup channel.
Definition network.c:106
int ff_network_sleep_interruptible(int64_t timeout, AVIOInterruptCB *int_cb)
Waits for up to 'timeout' microseconds.
Definition network.c:221
int ff_getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res)
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:333
void ff_log_net_error(void *ctx, int level, const char *prefix)
Definition network.c:706
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:381
int ff_network_wakeup_signal(FFNetWakeup *w)
Wake the thread blocked in ff_network_wait_fd_wakeup() on this channel, or the next one that waits on...
Definition network.c:162
void ff_network_wakeup_close(FFNetWakeup *w)
Close the channel.
Definition network.c:185
void ff_freeaddrinfo(struct addrinfo *res)
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:369
int ff_getnameinfo(const struct sockaddr *sa, int salen, char *host, int hostlen, char *serv, int servlen, int flags)
int ff_network_wait_fd(int fd, int write)
Definition network.c:77
int ff_network_init(void)
Initialize the network subsystem.
Definition network.c:66
int ff_udp_set_remote_addr(URLContext *h, const struct sockaddr *dest_addr, socklen_t dest_addr_len, int do_connect)
This function is identical to ff_udp_set_remote_url, except that it takes a sockaddr directly.
Definition udp.c:472
int ff_tls_init(void)
Definition network.c:42
void ff_udp_get_last_recv_addr(URLContext *h, struct sockaddr_storage *addr, socklen_t *addr_len)
Definition udp.c:510
#define ff_neterrno()
Definition network.h:69
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:86
void ff_network_close(void)
Definition network.c:239
void ff_tls_deinit(void)
Definition network.c:52
int ff_http_match_no_proxy(const char *no_proxy, const char *hostname)
Definition network.c:676
int ff_network_wait_fd_wakeup(int fd, int write, int timeout_ms, const FFNetWakeup *w)
Like ff_network_wait_fd(), but also returns as soon as the wakeup channel is signaled,...
Definition network.c:194
int ff_accept(int fd, int timeout, URLContext *h)
Poll for a single connection on the passed file descriptor.
Definition network.c:351
const char * ff_gai_strerror(int ecode)
miscellaneous OS support macros and functions.
int atomic_int
Definition stdatomic.h:63
Callback for checking whether to abort blocking functions.
Definition avio.h:59
A channel for waking a thread that is blocked in ff_network_wait_fd_wakeup(), from another thread.
Definition network.h:126
atomic_int signaled
Definition network.h:129
struct sockaddr * ai_addr
Definition network.h:206
char * ai_canonname
Definition network.h:207
int ai_socktype
Definition network.h:203
int ai_protocol
Definition network.h:204
int ai_addrlen
Definition network.h:205
struct addrinfo * ai_next
Definition network.h:208
int ai_flags
Definition network.h:201
int ai_family
Definition network.h:202
int64_t ss_align
Definition network.h:182
char ss_pad1[6]
Definition network.h:181
char ss_pad2[112]
Definition network.h:183
uint16_t ss_family
Definition network.h:179
uint8_t level
Definition svq3.c:208
static int customize_fd(void *ctx, int fd, int family)
Definition tcp.c:80
struct sockaddr_in in
Definition network.h:189
struct sockaddr_storage storage
Definition network.h:188
char prefix[8]
unbuffered private I/O API