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 extern int ff_network_inited_globally;
79 int ff_network_init(void);
80 void ff_network_close(void);
82 int ff_tls_init(void);
83 void ff_tls_deinit(void);
85 int ff_network_wait_fd(int fd, int write);
88 * This works similarly to ff_network_wait_fd, but waits up to 'timeout' microseconds
89 * Uses ff_network_wait_fd in a loop
91 * @fd Socket descriptor
92 * @write Set 1 to wait for socket able to be read, 0 to be written
93 * @timeout Timeout interval, in microseconds. Actual precision is 100000 mcs, due to ff_network_wait_fd usage
94 * @param int_cb Interrupt callback, is checked before each ff_network_wait_fd call
95 * @return 0 if data can be read/written, AVERROR(ETIMEDOUT) if timeout expired, or negative error code
97 int ff_network_wait_fd_timeout(int fd, int write, int64_t timeout, AVIOInterruptCB *int_cb);
99 int ff_inet_aton (const char * str, struct in_addr * add);
101 #if !HAVE_STRUCT_SOCKADDR_STORAGE
102 struct sockaddr_storage {
103 #if HAVE_STRUCT_SOCKADDR_SA_LEN
108 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
113 #endif /* !HAVE_STRUCT_SOCKADDR_STORAGE */
115 typedef union sockaddr_union {
116 struct sockaddr_storage storage;
117 struct sockaddr_in in;
118 #if HAVE_STRUCT_SOCKADDR_IN6
119 struct sockaddr_in6 in6;
124 #define MSG_NOSIGNAL 0
127 #if !HAVE_STRUCT_ADDRINFO
134 struct sockaddr *ai_addr;
136 struct addrinfo *ai_next;
138 #endif /* !HAVE_STRUCT_ADDRINFO */
140 /* getaddrinfo constants */
145 #define EAI_BADFLAGS 3
163 #define EAI_SERVICE 9
166 #define EAI_SOCKTYPE 10
174 #define AI_CANONNAME 2
177 #ifndef AI_NUMERICHOST
178 #define AI_NUMERICHOST 4
185 #ifndef NI_NUMERICHOST
186 #define NI_NUMERICHOST 2
193 #ifndef NI_NUMERICSERV
194 #define NI_NUMERICSERV 8
201 #if !HAVE_GETADDRINFO
202 int ff_getaddrinfo(const char *node, const char *service,
203 const struct addrinfo *hints, struct addrinfo **res);
204 void ff_freeaddrinfo(struct addrinfo *res);
205 int ff_getnameinfo(const struct sockaddr *sa, int salen,
206 char *host, int hostlen,
207 char *serv, int servlen, int flags);
208 #define getaddrinfo ff_getaddrinfo
209 #define freeaddrinfo ff_freeaddrinfo
210 #define getnameinfo ff_getnameinfo
211 #endif /* !HAVE_GETADDRINFO */
213 #if !HAVE_GETADDRINFO || HAVE_WINSOCK2_H
214 const char *ff_gai_strerror(int ecode);
216 #define gai_strerror ff_gai_strerror
217 #endif /* !HAVE_GETADDRINFO || HAVE_WINSOCK2_H */
219 #ifndef INADDR_LOOPBACK
220 #define INADDR_LOOPBACK 0x7f000001
223 #ifndef INET_ADDRSTRLEN
224 #define INET_ADDRSTRLEN 16
227 #ifndef INET6_ADDRSTRLEN
228 #define INET6_ADDRSTRLEN INET_ADDRSTRLEN
232 #define IN_MULTICAST(a) ((((uint32_t)(a)) & 0xf0000000) == 0xe0000000)
234 #ifndef IN6_IS_ADDR_MULTICAST
235 #define IN6_IS_ADDR_MULTICAST(a) (((uint8_t *) (a))[0] == 0xff)
238 int ff_is_multicast_address(struct sockaddr *addr);
240 #define POLLING_TIME 100 /// Time in milliseconds between interrupt check
243 * Bind to a file descriptor and poll for a connection.
245 * @param fd First argument of bind().
246 * @param addr Second argument of bind().
247 * @param addrlen Third argument of bind().
248 * @param timeout Polling timeout in milliseconds.
249 * @param h URLContext providing interrupt check
250 * callback and logging context.
251 * @return A non-blocking file descriptor on success
252 * or an AVERROR on failure.
254 int ff_listen_bind(int fd, const struct sockaddr *addr,
255 socklen_t addrlen, int timeout,
259 * Bind to a file descriptor to an address without accepting connections.
260 * @param fd First argument of bind().
261 * @param addr Second argument of bind().
262 * @param addrlen Third argument of bind().
263 * @return 0 on success or an AVERROR on failure.
265 int ff_listen(int fd, const struct sockaddr *addr, socklen_t addrlen);
268 * Poll for a single connection on the passed file descriptor.
269 * @param fd The listening socket file descriptor.
270 * @param timeout Polling timeout in milliseconds.
271 * @param h URLContext providing interrupt check
272 * callback and logging context.
273 * @return A non-blocking file descriptor on success
274 * or an AVERROR on failure.
276 int ff_accept(int fd, int timeout, URLContext *h);
279 * Connect to a file descriptor and poll for result.
281 * @param fd First argument of connect(),
282 * will be set as non-blocking.
283 * @param addr Second argument of connect().
284 * @param addrlen Third argument of connect().
285 * @param timeout Polling timeout in milliseconds.
286 * @param h URLContext providing interrupt check
287 * callback and logging context.
288 * @param will_try_next Whether the caller will try to connect to another
289 * address for the same host name, affecting the form of
291 * @return 0 on success, AVERROR on failure.
293 int ff_listen_connect(int fd, const struct sockaddr *addr,
294 socklen_t addrlen, int timeout,
295 URLContext *h, int will_try_next);
297 int ff_http_match_no_proxy(const char *no_proxy, const char *hostname);
299 int ff_socket(int domain, int type, int protocol);
301 #endif /* AVFORMAT_NETWORK_H */