]> git.sesse.net Git - ffmpeg/blob - libavformat/network.h
Merge commit '99e9697e3a12ab4a6638a36b95edafd6a98f9eaa'
[ffmpeg] / libavformat / network.h
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 <stdint.h>
26
27 #include "config.h"
28 #include "libavutil/error.h"
29 #include "os_support.h"
30 #include "avio.h"
31 #include "url.h"
32
33 #if HAVE_UNISTD_H
34 #include <unistd.h>
35 #endif
36
37 #if HAVE_WINSOCK2_H
38 #include <winsock2.h>
39 #include <ws2tcpip.h>
40
41 #ifndef EPROTONOSUPPORT
42 #define EPROTONOSUPPORT WSAEPROTONOSUPPORT
43 #endif
44 #ifndef ETIMEDOUT
45 #define ETIMEDOUT       WSAETIMEDOUT
46 #endif
47 #ifndef ECONNREFUSED
48 #define ECONNREFUSED    WSAECONNREFUSED
49 #endif
50 #ifndef EINPROGRESS
51 #define EINPROGRESS     WSAEINPROGRESS
52 #endif
53
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)
56
57 int ff_neterrno(void);
58 #else
59 #include <sys/types.h>
60 #include <sys/socket.h>
61 #include <netinet/in.h>
62 #include <netinet/tcp.h>
63 #include <netdb.h>
64
65 #define ff_neterrno() AVERROR(errno)
66 #endif /* HAVE_WINSOCK2_H */
67
68 #if HAVE_ARPA_INET_H
69 #include <arpa/inet.h>
70 #endif
71
72 #if HAVE_POLL_H
73 #include <poll.h>
74 #endif
75
76 int ff_socket_nonblock(int socket, int enable);
77
78 extern int ff_network_inited_globally;
79 int ff_network_init(void);
80 void ff_network_close(void);
81
82 int ff_tls_init(void);
83 void ff_tls_deinit(void);
84
85 int ff_network_wait_fd(int fd, int write);
86
87 /**
88  * This works similarly to ff_network_wait_fd, but waits up to 'timeout' microseconds
89  * Uses ff_network_wait_fd in a loop
90  *
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
96  */
97 int ff_network_wait_fd_timeout(int fd, int write, int64_t timeout, AVIOInterruptCB *int_cb);
98
99 int ff_inet_aton (const char * str, struct in_addr * add);
100
101 #if !HAVE_STRUCT_SOCKADDR_STORAGE
102 struct sockaddr_storage {
103 #if HAVE_STRUCT_SOCKADDR_SA_LEN
104     uint8_t ss_len;
105     uint8_t ss_family;
106 #else
107     uint16_t ss_family;
108 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
109     char ss_pad1[6];
110     int64_t ss_align;
111     char ss_pad2[112];
112 };
113 #endif /* !HAVE_STRUCT_SOCKADDR_STORAGE */
114
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;
120 #endif
121 } sockaddr_union;
122
123 #ifndef MSG_NOSIGNAL
124 #define MSG_NOSIGNAL 0
125 #endif
126
127 #if !HAVE_STRUCT_ADDRINFO
128 struct addrinfo {
129     int ai_flags;
130     int ai_family;
131     int ai_socktype;
132     int ai_protocol;
133     int ai_addrlen;
134     struct sockaddr *ai_addr;
135     char *ai_canonname;
136     struct addrinfo *ai_next;
137 };
138 #endif /* !HAVE_STRUCT_ADDRINFO */
139
140 /* getaddrinfo constants */
141 #ifndef EAI_AGAIN
142 #define EAI_AGAIN 2
143 #endif
144 #ifndef EAI_BADFLAGS
145 #define EAI_BADFLAGS 3
146 #endif
147 #ifndef EAI_FAIL
148 #define EAI_FAIL 4
149 #endif
150 #ifndef EAI_FAMILY
151 #define EAI_FAMILY 5
152 #endif
153 #ifndef EAI_MEMORY
154 #define EAI_MEMORY 6
155 #endif
156 #ifndef EAI_NODATA
157 #define EAI_NODATA 7
158 #endif
159 #ifndef EAI_NONAME
160 #define EAI_NONAME 8
161 #endif
162 #ifndef EAI_SERVICE
163 #define EAI_SERVICE 9
164 #endif
165 #ifndef EAI_SOCKTYPE
166 #define EAI_SOCKTYPE 10
167 #endif
168
169 #ifndef AI_PASSIVE
170 #define AI_PASSIVE 1
171 #endif
172
173 #ifndef AI_CANONNAME
174 #define AI_CANONNAME 2
175 #endif
176
177 #ifndef AI_NUMERICHOST
178 #define AI_NUMERICHOST 4
179 #endif
180
181 #ifndef NI_NOFQDN
182 #define NI_NOFQDN 1
183 #endif
184
185 #ifndef NI_NUMERICHOST
186 #define NI_NUMERICHOST 2
187 #endif
188
189 #ifndef NI_NAMERQD
190 #define NI_NAMERQD 4
191 #endif
192
193 #ifndef NI_NUMERICSERV
194 #define NI_NUMERICSERV 8
195 #endif
196
197 #ifndef NI_DGRAM
198 #define NI_DGRAM 16
199 #endif
200
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 */
212
213 #if !HAVE_GETADDRINFO || HAVE_WINSOCK2_H
214 const char *ff_gai_strerror(int ecode);
215 #undef gai_strerror
216 #define gai_strerror ff_gai_strerror
217 #endif /* !HAVE_GETADDRINFO || HAVE_WINSOCK2_H */
218
219 #ifndef INADDR_LOOPBACK
220 #define INADDR_LOOPBACK 0x7f000001
221 #endif
222
223 #ifndef INET_ADDRSTRLEN
224 #define INET_ADDRSTRLEN 16
225 #endif
226
227 #ifndef INET6_ADDRSTRLEN
228 #define INET6_ADDRSTRLEN INET_ADDRSTRLEN
229 #endif
230
231 #ifndef IN_MULTICAST
232 #define IN_MULTICAST(a) ((((uint32_t)(a)) & 0xf0000000) == 0xe0000000)
233 #endif
234 #ifndef IN6_IS_ADDR_MULTICAST
235 #define IN6_IS_ADDR_MULTICAST(a) (((uint8_t *) (a))[0] == 0xff)
236 #endif
237
238 int ff_is_multicast_address(struct sockaddr *addr);
239
240 #define POLLING_TIME 100 /// Time in milliseconds between interrupt check
241
242 /**
243  * Bind to a file descriptor and poll for a connection.
244  *
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.
253  */
254 int ff_listen_bind(int fd, const struct sockaddr *addr,
255                    socklen_t addrlen, int timeout,
256                    URLContext *h);
257
258 /**
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.
264  */
265 int ff_listen(int fd, const struct sockaddr *addr, socklen_t addrlen);
266
267 /**
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.
275  */
276 int ff_accept(int fd, int timeout, URLContext *h);
277
278 /**
279  * Connect to a file descriptor and poll for result.
280  *
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
290  *                 logged errors.
291  * @return         0 on success, AVERROR on failure.
292  */
293 int ff_listen_connect(int fd, const struct sockaddr *addr,
294                       socklen_t addrlen, int timeout,
295                       URLContext *h, int will_try_next);
296
297 int ff_http_match_no_proxy(const char *no_proxy, const char *hostname);
298
299 int ff_socket(int domain, int type, int protocol);
300
301 #endif /* AVFORMAT_NETWORK_H */