2 * Copyright (c) 2007 The FFmpeg Project
4 * This file is part of FFmpeg.
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.
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.
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
21 #ifndef AVFORMAT_NETWORK_H
22 #define AVFORMAT_NETWORK_H
28 #include "libavutil/error.h"
29 #include "os_support.h"
41 #ifndef EPROTONOSUPPORT
42 #define EPROTONOSUPPORT WSAEPROTONOSUPPORT
45 #define ETIMEDOUT WSAETIMEDOUT
48 #define ECONNREFUSED WSAECONNREFUSED
51 #define EINPROGRESS WSAEINPROGRESS
54 #define ENOTCONN WSAENOTCONN
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)
60 int ff_neterrno(void);
62 #include <sys/types.h>
63 #include <sys/socket.h>
64 #include <netinet/in.h>
65 #include <netinet/tcp.h>
68 #define ff_neterrno() AVERROR(errno)
69 #endif /* HAVE_WINSOCK2_H */
72 #include <arpa/inet.h>
79 int ff_socket_nonblock(int socket, int enable);
81 int ff_network_init(void);
82 void ff_network_close(void);
84 int ff_tls_init(void);
85 void ff_tls_deinit(void);
87 int ff_network_wait_fd(int fd, int write);
90 * This works similarly to ff_network_wait_fd, but waits up to 'timeout' microseconds
91 * Uses ff_network_wait_fd in a loop
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
99 int ff_network_wait_fd_timeout(int fd, int write, int64_t timeout, AVIOInterruptCB *int_cb);
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
108 int ff_network_sleep_interruptible(int64_t timeout, AVIOInterruptCB *int_cb);
110 #if !HAVE_STRUCT_SOCKADDR_STORAGE
111 struct sockaddr_storage {
112 #if HAVE_STRUCT_SOCKADDR_SA_LEN
117 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
122 #endif /* !HAVE_STRUCT_SOCKADDR_STORAGE */
124 typedef union sockaddr_union {
125 struct sockaddr_storage storage;
126 struct sockaddr_in in;
127 #if HAVE_STRUCT_SOCKADDR_IN6
128 struct sockaddr_in6 in6;
133 #define MSG_NOSIGNAL 0
136 #if !HAVE_STRUCT_ADDRINFO
143 struct sockaddr *ai_addr;
145 struct addrinfo *ai_next;
147 #endif /* !HAVE_STRUCT_ADDRINFO */
149 /* getaddrinfo constants */
154 #define EAI_BADFLAGS 3
172 #define EAI_SERVICE 9
175 #define EAI_SOCKTYPE 10
183 #define AI_CANONNAME 2
186 #ifndef AI_NUMERICHOST
187 #define AI_NUMERICHOST 4
194 #ifndef NI_NUMERICHOST
195 #define NI_NUMERICHOST 2
202 #ifndef NI_NUMERICSERV
203 #define NI_NUMERICSERV 8
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 */
222 #if !HAVE_GETADDRINFO || HAVE_WINSOCK2_H
223 const char *ff_gai_strerror(int ecode);
225 #define gai_strerror ff_gai_strerror
226 #endif /* !HAVE_GETADDRINFO || HAVE_WINSOCK2_H */
228 #ifndef INADDR_LOOPBACK
229 #define INADDR_LOOPBACK 0x7f000001
232 #ifndef INET_ADDRSTRLEN
233 #define INET_ADDRSTRLEN 16
236 #ifndef INET6_ADDRSTRLEN
237 #define INET6_ADDRSTRLEN INET_ADDRSTRLEN
241 #define IN_MULTICAST(a) ((((uint32_t)(a)) & 0xf0000000) == 0xe0000000)
243 #ifndef IN6_IS_ADDR_MULTICAST
244 #define IN6_IS_ADDR_MULTICAST(a) (((uint8_t *) (a))[0] == 0xff)
247 int ff_is_multicast_address(struct sockaddr *addr);
249 #define POLLING_TIME 100 /// Time in milliseconds between interrupt check
252 * Bind to a file descriptor and poll for a connection.
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.
263 int ff_listen_bind(int fd, const struct sockaddr *addr,
264 socklen_t addrlen, int timeout,
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.
274 int ff_listen(int fd, const struct sockaddr *addr, socklen_t addrlen);
277 * Poll for a single connection on the passed file descriptor.
278 * @param fd The listening socket file descriptor.
279 * @param timeout Polling timeout in milliseconds.
280 * @param h URLContext providing interrupt check
281 * callback and logging context.
282 * @return A non-blocking file descriptor on success
283 * or an AVERROR on failure.
285 int ff_accept(int fd, int timeout, URLContext *h);
288 * Connect to a file descriptor and poll for result.
290 * @param fd First argument of connect(),
291 * will be set as non-blocking.
292 * @param addr Second argument of connect().
293 * @param addrlen Third argument of connect().
294 * @param timeout Polling timeout in milliseconds.
295 * @param h URLContext providing interrupt check
296 * callback and logging context.
297 * @param will_try_next Whether the caller will try to connect to another
298 * address for the same host name, affecting the form of
300 * @return 0 on success, AVERROR on failure.
302 int ff_listen_connect(int fd, const struct sockaddr *addr,
303 socklen_t addrlen, int timeout,
304 URLContext *h, int will_try_next);
306 int ff_http_match_no_proxy(const char *no_proxy, const char *hostname);
308 int ff_socket(int domain, int type, int protocol);
310 void ff_log_net_error(void *ctx, int level, const char* prefix);
313 * Connect to any of the given addrinfo addresses, with multiple attempts
314 * running in parallel.
316 * @param addrs The list of addresses to try to connect to.
317 * This list will be mutated internally, but the list head
318 * will remain as such, so this doesn't affect the caller
319 * freeing the list afterwards.
320 * @param timeout_ms_per_address The number of milliseconds to wait for each
321 * connection attempt. Since multiple addresses are tried,
322 * some of them in parallel, the total run time will at most
323 * be timeout_ms_per_address*ceil(nb_addrs/parallel) +
324 * (parallel - 1) * NEXT_ATTEMPT_DELAY_MS.
325 * @param parallel The maximum number of connections to attempt in parallel.
326 * This is limited to an internal maximum capacity.
327 * @param h URLContext providing interrupt check
328 * callback and logging context.
329 * @param fd If successful, the connected socket is returned here.
330 * @param customize_fd Function that will be called for each socket created,
331 * to allow the caller to set socket options before calling
332 * connect() on it, may be NULL.
333 * @param customize_ctx Context parameter passed to customize_fd.
334 * @return 0 on success, AVERROR on failure.
336 int ff_connect_parallel(struct addrinfo *addrs, int timeout_ms_per_address,
337 int parallel, URLContext *h, int *fd,
338 void (*customize_fd)(void *, int), void *customize_ctx);
340 #endif /* AVFORMAT_NETWORK_H */