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 getsockopt(a, b, c, d, e) getsockopt(a, b, c, (char*) d, e)
55 #define setsockopt(a, b, c, d, e) setsockopt(a, b, c, (const char*) d, e)
57 int ff_neterrno(void);
59 #include <sys/types.h>
60 #include <sys/socket.h>
61 #include <netinet/in.h>
62 #include <netinet/tcp.h>
65 #define ff_neterrno() AVERROR(errno)
66 #endif /* HAVE_WINSOCK2_H */
69 #include <arpa/inet.h>
76 int ff_socket_nonblock(int socket, int enable);
78 int ff_network_init(void);
79 void ff_network_close(void);
81 int ff_tls_init(void);
82 void ff_tls_deinit(void);
84 int ff_network_wait_fd(int fd, int write);
87 * This works similarly to ff_network_wait_fd, but waits up to 'timeout' microseconds
88 * Uses ff_network_wait_fd in a loop
90 * @param fd Socket descriptor
91 * @param write Set 1 to wait for socket able to be read, 0 to be written
92 * @param timeout Timeout interval, in microseconds. Actual precision is 100000 mcs, due to ff_network_wait_fd usage
93 * @param int_cb Interrupt callback, is checked before each ff_network_wait_fd call
94 * @return 0 if data can be read/written, AVERROR(ETIMEDOUT) if timeout expired, or negative error code
96 int ff_network_wait_fd_timeout(int fd, int write, int64_t timeout, AVIOInterruptCB *int_cb);
99 * Waits for up to 'timeout' microseconds. If the usert's int_cb is set and
100 * triggered, return before that.
101 * @param timeout Timeout in microseconds. Maybe have lower actual precision.
102 * @param int_cb Interrupt callback, is checked regularly.
103 * @return AVERROR(ETIMEDOUT) if timeout expirted, AVERROR_EXIT if interrupted by int_cb
105 int ff_network_sleep_interruptible(int64_t timeout, AVIOInterruptCB *int_cb);
107 #if !HAVE_STRUCT_SOCKADDR_STORAGE
108 struct sockaddr_storage {
109 #if HAVE_STRUCT_SOCKADDR_SA_LEN
114 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
119 #endif /* !HAVE_STRUCT_SOCKADDR_STORAGE */
121 typedef union sockaddr_union {
122 struct sockaddr_storage storage;
123 struct sockaddr_in in;
124 #if HAVE_STRUCT_SOCKADDR_IN6
125 struct sockaddr_in6 in6;
130 #define MSG_NOSIGNAL 0
133 #if !HAVE_STRUCT_ADDRINFO
140 struct sockaddr *ai_addr;
142 struct addrinfo *ai_next;
144 #endif /* !HAVE_STRUCT_ADDRINFO */
146 /* getaddrinfo constants */
151 #define EAI_BADFLAGS 3
169 #define EAI_SERVICE 9
172 #define EAI_SOCKTYPE 10
180 #define AI_CANONNAME 2
183 #ifndef AI_NUMERICHOST
184 #define AI_NUMERICHOST 4
191 #ifndef NI_NUMERICHOST
192 #define NI_NUMERICHOST 2
199 #ifndef NI_NUMERICSERV
200 #define NI_NUMERICSERV 8
207 #if !HAVE_GETADDRINFO
208 int ff_getaddrinfo(const char *node, const char *service,
209 const struct addrinfo *hints, struct addrinfo **res);
210 void ff_freeaddrinfo(struct addrinfo *res);
211 int ff_getnameinfo(const struct sockaddr *sa, int salen,
212 char *host, int hostlen,
213 char *serv, int servlen, int flags);
214 #define getaddrinfo ff_getaddrinfo
215 #define freeaddrinfo ff_freeaddrinfo
216 #define getnameinfo ff_getnameinfo
217 #endif /* !HAVE_GETADDRINFO */
219 #if !HAVE_GETADDRINFO || HAVE_WINSOCK2_H
220 const char *ff_gai_strerror(int ecode);
222 #define gai_strerror ff_gai_strerror
223 #endif /* !HAVE_GETADDRINFO || HAVE_WINSOCK2_H */
225 #ifndef INADDR_LOOPBACK
226 #define INADDR_LOOPBACK 0x7f000001
229 #ifndef INET_ADDRSTRLEN
230 #define INET_ADDRSTRLEN 16
233 #ifndef INET6_ADDRSTRLEN
234 #define INET6_ADDRSTRLEN INET_ADDRSTRLEN
238 #define IN_MULTICAST(a) ((((uint32_t)(a)) & 0xf0000000) == 0xe0000000)
240 #ifndef IN6_IS_ADDR_MULTICAST
241 #define IN6_IS_ADDR_MULTICAST(a) (((uint8_t *) (a))[0] == 0xff)
244 int ff_is_multicast_address(struct sockaddr *addr);
246 #define POLLING_TIME 100 /// Time in milliseconds between interrupt check
249 * Bind to a file descriptor and poll for a connection.
251 * @param fd First argument of bind().
252 * @param addr Second argument of bind().
253 * @param addrlen Third argument of bind().
254 * @param timeout Polling timeout in milliseconds.
255 * @param h URLContext providing interrupt check
256 * callback and logging context.
257 * @return A non-blocking file descriptor on success
258 * or an AVERROR on failure.
260 int ff_listen_bind(int fd, const struct sockaddr *addr,
261 socklen_t addrlen, int timeout,
265 * Bind to a file descriptor to an address without accepting connections.
266 * @param fd First argument of bind().
267 * @param addr Second argument of bind().
268 * @param addrlen Third argument of bind().
269 * @return 0 on success or an AVERROR on failure.
271 int ff_listen(int fd, const struct sockaddr *addr, socklen_t addrlen);
274 * Poll for a single connection on the passed file descriptor.
275 * @param fd The listening socket file descriptor.
276 * @param timeout Polling timeout in milliseconds.
277 * @param h URLContext providing interrupt check
278 * callback and logging context.
279 * @return A non-blocking file descriptor on success
280 * or an AVERROR on failure.
282 int ff_accept(int fd, int timeout, URLContext *h);
285 * Connect to a file descriptor and poll for result.
287 * @param fd First argument of connect(),
288 * will be set as non-blocking.
289 * @param addr Second argument of connect().
290 * @param addrlen Third argument of connect().
291 * @param timeout Polling timeout in milliseconds.
292 * @param h URLContext providing interrupt check
293 * callback and logging context.
294 * @param will_try_next Whether the caller will try to connect to another
295 * address for the same host name, affecting the form of
297 * @return 0 on success, AVERROR on failure.
299 int ff_listen_connect(int fd, const struct sockaddr *addr,
300 socklen_t addrlen, int timeout,
301 URLContext *h, int will_try_next);
303 int ff_http_match_no_proxy(const char *no_proxy, const char *hostname);
305 int ff_socket(int domain, int type, int protocol);
307 #endif /* AVFORMAT_NETWORK_H */