]> git.sesse.net Git - ffmpeg/blob - libavformat/network.h
Merge commit 'f79d847400d218cfd0b95f10358fe6e65ec3c9c4'
[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 <netdb.h>
63
64 #define ff_neterrno() AVERROR(errno)
65 #endif /* HAVE_WINSOCK2_H */
66
67 #if HAVE_ARPA_INET_H
68 #include <arpa/inet.h>
69 #endif
70
71 #if HAVE_POLL_H
72 #include <poll.h>
73 #endif
74
75 int ff_socket_nonblock(int socket, int enable);
76
77 extern int ff_network_inited_globally;
78 int ff_network_init(void);
79 void ff_network_close(void);
80
81 int ff_tls_init(void);
82 void ff_tls_deinit(void);
83
84 int ff_network_wait_fd(int fd, int write);
85
86 /**
87  * This works similarly to ff_network_wait_fd, but waits up to 'timeout' microseconds
88  * Uses ff_network_wait_fd in a loop
89  *
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
95  */
96 int ff_network_wait_fd_timeout(int fd, int write, int64_t timeout, AVIOInterruptCB *int_cb);
97
98 int ff_inet_aton (const char * str, struct in_addr * add);
99
100 #if !HAVE_STRUCT_SOCKADDR_STORAGE
101 struct sockaddr_storage {
102 #if HAVE_STRUCT_SOCKADDR_SA_LEN
103     uint8_t ss_len;
104     uint8_t ss_family;
105 #else
106     uint16_t ss_family;
107 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
108     char ss_pad1[6];
109     int64_t ss_align;
110     char ss_pad2[112];
111 };
112 #endif /* !HAVE_STRUCT_SOCKADDR_STORAGE */
113
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;
119 #endif
120 } sockaddr_union;
121
122 #ifndef MSG_NOSIGNAL
123 #define MSG_NOSIGNAL 0
124 #endif
125
126 #if !HAVE_STRUCT_ADDRINFO
127 struct addrinfo {
128     int ai_flags;
129     int ai_family;
130     int ai_socktype;
131     int ai_protocol;
132     int ai_addrlen;
133     struct sockaddr *ai_addr;
134     char *ai_canonname;
135     struct addrinfo *ai_next;
136 };
137 #endif /* !HAVE_STRUCT_ADDRINFO */
138
139 /* getaddrinfo constants */
140 #ifndef EAI_AGAIN
141 #define EAI_AGAIN 2
142 #endif
143 #ifndef EAI_BADFLAGS
144 #define EAI_BADFLAGS 3
145 #endif
146 #ifndef EAI_FAIL
147 #define EAI_FAIL 4
148 #endif
149 #ifndef EAI_FAMILY
150 #define EAI_FAMILY 5
151 #endif
152 #ifndef EAI_MEMORY
153 #define EAI_MEMORY 6
154 #endif
155 #ifndef EAI_NODATA
156 #define EAI_NODATA 7
157 #endif
158 #ifndef EAI_NONAME
159 #define EAI_NONAME 8
160 #endif
161 #ifndef EAI_SERVICE
162 #define EAI_SERVICE 9
163 #endif
164 #ifndef EAI_SOCKTYPE
165 #define EAI_SOCKTYPE 10
166 #endif
167
168 #ifndef AI_PASSIVE
169 #define AI_PASSIVE 1
170 #endif
171
172 #ifndef AI_CANONNAME
173 #define AI_CANONNAME 2
174 #endif
175
176 #ifndef AI_NUMERICHOST
177 #define AI_NUMERICHOST 4
178 #endif
179
180 #ifndef NI_NOFQDN
181 #define NI_NOFQDN 1
182 #endif
183
184 #ifndef NI_NUMERICHOST
185 #define NI_NUMERICHOST 2
186 #endif
187
188 #ifndef NI_NAMERQD
189 #define NI_NAMERQD 4
190 #endif
191
192 #ifndef NI_NUMERICSERV
193 #define NI_NUMERICSERV 8
194 #endif
195
196 #ifndef NI_DGRAM
197 #define NI_DGRAM 16
198 #endif
199
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 */
211
212 #if !HAVE_GETADDRINFO || HAVE_WINSOCK2_H
213 const char *ff_gai_strerror(int ecode);
214 #undef gai_strerror
215 #define gai_strerror ff_gai_strerror
216 #endif /* !HAVE_GETADDRINFO || HAVE_WINSOCK2_H */
217
218 #ifndef INADDR_LOOPBACK
219 #define INADDR_LOOPBACK 0x7f000001
220 #endif
221
222 #ifndef INET_ADDRSTRLEN
223 #define INET_ADDRSTRLEN 16
224 #endif
225
226 #ifndef INET6_ADDRSTRLEN
227 #define INET6_ADDRSTRLEN INET_ADDRSTRLEN
228 #endif
229
230 #ifndef IN_MULTICAST
231 #define IN_MULTICAST(a) ((((uint32_t)(a)) & 0xf0000000) == 0xe0000000)
232 #endif
233 #ifndef IN6_IS_ADDR_MULTICAST
234 #define IN6_IS_ADDR_MULTICAST(a) (((uint8_t *) (a))[0] == 0xff)
235 #endif
236
237 int ff_is_multicast_address(struct sockaddr *addr);
238
239 #define POLLING_TIME 100 /// Time in milliseconds between interrupt check
240
241 /**
242  * Bind to a file descriptor and poll for a connection.
243  *
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.
252  */
253 int ff_listen_bind(int fd, const struct sockaddr *addr,
254                    socklen_t addrlen, int timeout,
255                    URLContext *h);
256
257 /**
258  * Bind to a file descriptor to an address without accepting connections.
259  * @param fd      First argument of bind().
260  * @param addr    Second argument of bind().
261  * @param addrlen Third argument of bind().
262  * @return        0 on success or an AVERROR on failure.
263  */
264 int ff_listen(int fd, const struct sockaddr *addr, socklen_t addrlen);
265
266 /**
267  * Poll for a single connection on the passed file descriptor.
268  * @param fd      The listening socket file descriptor.
269  * @param timeout Polling timeout in milliseconds.
270  * @param h       URLContext providing interrupt check
271  *                callback and logging context.
272  * @return        A non-blocking file descriptor on success
273  *                or an AVERROR on failure.
274  */
275 int ff_accept(int fd, int timeout, URLContext *h);
276
277 /**
278  * Connect to a file descriptor and poll for result.
279  *
280  * @param fd       First argument of connect(),
281  *                 will be set as non-blocking.
282  * @param addr     Second argument of connect().
283  * @param addrlen  Third argument of connect().
284  * @param timeout  Polling timeout in milliseconds.
285  * @param h        URLContext providing interrupt check
286  *                 callback and logging context.
287  * @param will_try_next Whether the caller will try to connect to another
288  *                 address for the same host name, affecting the form of
289  *                 logged errors.
290  * @return         0 on success, AVERROR on failure.
291  */
292 int ff_listen_connect(int fd, const struct sockaddr *addr,
293                       socklen_t addrlen, int timeout,
294                       URLContext *h, int will_try_next);
295
296 int ff_http_match_no_proxy(const char *no_proxy, const char *hostname);
297
298 int ff_socket(int domain, int type, int protocol);
299
300 #endif /* AVFORMAT_NETWORK_H */