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>
64 #define ff_neterrno() AVERROR(errno)
65 #endif /* HAVE_WINSOCK2_H */
68 #include <arpa/inet.h>
75 int ff_socket_nonblock(int socket, int enable);
77 extern int ff_network_inited_globally;
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 * @fd Socket descriptor
91 * @write Set 1 to wait for socket able to be read, 0 to be written
92 * @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);
98 int ff_inet_aton (const char * str, struct in_addr * add);
100 #if !HAVE_STRUCT_SOCKADDR_STORAGE
101 struct sockaddr_storage {
102 #if HAVE_STRUCT_SOCKADDR_SA_LEN
107 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
112 #endif /* !HAVE_STRUCT_SOCKADDR_STORAGE */
114 typedef union sockaddr_union {
115 struct sockaddr_storage storage;
116 struct sockaddr_in in;
117 #if HAVE_STRUCT_SOCKADDR_IN6
118 struct sockaddr_in6 in6;
123 #define MSG_NOSIGNAL 0
126 #if !HAVE_STRUCT_ADDRINFO
133 struct sockaddr *ai_addr;
135 struct addrinfo *ai_next;
137 #endif /* !HAVE_STRUCT_ADDRINFO */
139 /* getaddrinfo constants */
144 #define EAI_BADFLAGS 3
162 #define EAI_SERVICE 9
165 #define EAI_SOCKTYPE 10
173 #define AI_CANONNAME 2
176 #ifndef AI_NUMERICHOST
177 #define AI_NUMERICHOST 4
184 #ifndef NI_NUMERICHOST
185 #define NI_NUMERICHOST 2
192 #ifndef NI_NUMERICSERV
193 #define NI_NUMERICSERV 8
200 #if !HAVE_GETADDRINFO
201 int ff_getaddrinfo(const char *node, const char *service,
202 const struct addrinfo *hints, struct addrinfo **res);
203 void ff_freeaddrinfo(struct addrinfo *res);
204 int ff_getnameinfo(const struct sockaddr *sa, int salen,
205 char *host, int hostlen,
206 char *serv, int servlen, int flags);
207 #define getaddrinfo ff_getaddrinfo
208 #define freeaddrinfo ff_freeaddrinfo
209 #define getnameinfo ff_getnameinfo
210 #endif /* !HAVE_GETADDRINFO */
212 #if !HAVE_GETADDRINFO || HAVE_WINSOCK2_H
213 const char *ff_gai_strerror(int ecode);
215 #define gai_strerror ff_gai_strerror
216 #endif /* !HAVE_GETADDRINFO || HAVE_WINSOCK2_H */
218 #ifndef INADDR_LOOPBACK
219 #define INADDR_LOOPBACK 0x7f000001
222 #ifndef INET_ADDRSTRLEN
223 #define INET_ADDRSTRLEN 16
226 #ifndef INET6_ADDRSTRLEN
227 #define INET6_ADDRSTRLEN INET_ADDRSTRLEN
231 #define IN_MULTICAST(a) ((((uint32_t)(a)) & 0xf0000000) == 0xe0000000)
233 #ifndef IN6_IS_ADDR_MULTICAST
234 #define IN6_IS_ADDR_MULTICAST(a) (((uint8_t *) (a))[0] == 0xff)
237 int ff_is_multicast_address(struct sockaddr *addr);
239 #define POLLING_TIME 100 /// Time in milliseconds between interrupt check
242 * Bind to a file descriptor and poll for a connection.
244 * @param fd First argument of bind().
245 * @param addr Second argument of bind().
246 * @param addrlen Third argument of bind().
247 * @param timeout Polling timeout in milliseconds.
248 * @param h URLContext providing interrupt check
249 * callback and logging context.
250 * @return A non-blocking file descriptor on success
251 * or an AVERROR on failure.
253 int ff_listen_bind(int fd, const struct sockaddr *addr,
254 socklen_t addrlen, int timeout,
258 * Connect to a file descriptor and poll for result.
260 * @param fd First argument of connect(),
261 * will be set as non-blocking.
262 * @param addr Second argument of connect().
263 * @param addrlen Third argument of connect().
264 * @param timeout Polling timeout in milliseconds.
265 * @param h URLContext providing interrupt check
266 * callback and logging context.
267 * @param will_try_next Whether the caller will try to connect to another
268 * address for the same host name, affecting the form of
270 * @return 0 on success, AVERROR on failure.
272 int ff_listen_connect(int fd, const struct sockaddr *addr,
273 socklen_t addrlen, int timeout,
274 URLContext *h, int will_try_next);
276 int ff_http_match_no_proxy(const char *no_proxy, const char *hostname);
278 int ff_socket(int domain, int type, int protocol);
280 #endif /* AVFORMAT_NETWORK_H */