FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
network.h
Go to the documentation of this file.
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 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 /**
99  * Waits for up to 'timeout' microseconds. If the usert's int_cb is set and
100  * triggered, return before that.
101  * @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
104  */
106 
107 #if !HAVE_STRUCT_SOCKADDR_STORAGE
109 #if HAVE_STRUCT_SOCKADDR_SA_LEN
110  uint8_t ss_len;
112 #else
113  uint16_t ss_family;
114 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
115  char ss_pad1[6];
116  int64_t ss_align;
117  char ss_pad2[112];
118 };
119 #endif /* !HAVE_STRUCT_SOCKADDR_STORAGE */
120 
121 typedef union sockaddr_union {
123  struct sockaddr_in in;
124 #if HAVE_STRUCT_SOCKADDR_IN6
125  struct sockaddr_in6 in6;
126 #endif
128 
129 #ifndef MSG_NOSIGNAL
130 #define MSG_NOSIGNAL 0
131 #endif
132 
133 #if !HAVE_STRUCT_ADDRINFO
134 struct addrinfo {
135  int ai_flags;
140  struct sockaddr *ai_addr;
142  struct addrinfo *ai_next;
143 };
144 #endif /* !HAVE_STRUCT_ADDRINFO */
145 
146 /* getaddrinfo constants */
147 #ifndef EAI_AGAIN
148 #define EAI_AGAIN 2
149 #endif
150 #ifndef EAI_BADFLAGS
151 #define EAI_BADFLAGS 3
152 #endif
153 #ifndef EAI_FAIL
154 #define EAI_FAIL 4
155 #endif
156 #ifndef EAI_FAMILY
157 #define EAI_FAMILY 5
158 #endif
159 #ifndef EAI_MEMORY
160 #define EAI_MEMORY 6
161 #endif
162 #ifndef EAI_NODATA
163 #define EAI_NODATA 7
164 #endif
165 #ifndef EAI_NONAME
166 #define EAI_NONAME 8
167 #endif
168 #ifndef EAI_SERVICE
169 #define EAI_SERVICE 9
170 #endif
171 #ifndef EAI_SOCKTYPE
172 #define EAI_SOCKTYPE 10
173 #endif
174 
175 #ifndef AI_PASSIVE
176 #define AI_PASSIVE 1
177 #endif
178 
179 #ifndef AI_CANONNAME
180 #define AI_CANONNAME 2
181 #endif
182 
183 #ifndef AI_NUMERICHOST
184 #define AI_NUMERICHOST 4
185 #endif
186 
187 #ifndef NI_NOFQDN
188 #define NI_NOFQDN 1
189 #endif
190 
191 #ifndef NI_NUMERICHOST
192 #define NI_NUMERICHOST 2
193 #endif
194 
195 #ifndef NI_NAMERQD
196 #define NI_NAMERQD 4
197 #endif
198 
199 #ifndef NI_NUMERICSERV
200 #define NI_NUMERICSERV 8
201 #endif
202 
203 #ifndef NI_DGRAM
204 #define NI_DGRAM 16
205 #endif
206 
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 */
218 
219 #if !HAVE_GETADDRINFO || HAVE_WINSOCK2_H
220 const char *ff_gai_strerror(int ecode);
221 #undef gai_strerror
222 #define gai_strerror ff_gai_strerror
223 #endif /* !HAVE_GETADDRINFO || HAVE_WINSOCK2_H */
224 
225 #ifndef INADDR_LOOPBACK
226 #define INADDR_LOOPBACK 0x7f000001
227 #endif
228 
229 #ifndef INET_ADDRSTRLEN
230 #define INET_ADDRSTRLEN 16
231 #endif
232 
233 #ifndef INET6_ADDRSTRLEN
234 #define INET6_ADDRSTRLEN INET_ADDRSTRLEN
235 #endif
236 
237 #ifndef IN_MULTICAST
238 #define IN_MULTICAST(a) ((((uint32_t)(a)) & 0xf0000000) == 0xe0000000)
239 #endif
240 #ifndef IN6_IS_ADDR_MULTICAST
241 #define IN6_IS_ADDR_MULTICAST(a) (((uint8_t *) (a))[0] == 0xff)
242 #endif
243 
244 int ff_is_multicast_address(struct sockaddr *addr);
245 
246 #define POLLING_TIME 100 /// Time in milliseconds between interrupt check
247 
248 /**
249  * Bind to a file descriptor and poll for a connection.
250  *
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.
259  */
260 int ff_listen_bind(int fd, const struct sockaddr *addr,
261  socklen_t addrlen, int timeout,
262  URLContext *h);
263 
264 /**
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.
270  */
271 int ff_listen(int fd, const struct sockaddr *addr, socklen_t addrlen);
272 
273 /**
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.
281  */
282 int ff_accept(int fd, int timeout, URLContext *h);
283 
284 /**
285  * Connect to a file descriptor and poll for result.
286  *
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
296  * logged errors.
297  * @return 0 on success, AVERROR on failure.
298  */
299 int ff_listen_connect(int fd, const struct sockaddr *addr,
300  socklen_t addrlen, int timeout,
301  URLContext *h, int will_try_next);
302 
303 int ff_http_match_no_proxy(const char *no_proxy, const char *hostname);
304 
305 int ff_socket(int domain, int type, int protocol);
306 
307 #endif /* AVFORMAT_NETWORK_H */
Buffered I/O operations.
char ss_pad1[6]
Definition: network.h:115
int ff_network_wait_fd(int fd, int write)
Definition: network.c:68
int ff_getnameinfo(const struct sockaddr *sa, int salen, char *host, int hostlen, char *serv, int servlen, int flags)
int ff_is_multicast_address(struct sockaddr *addr)
Definition: network.c:144
int ff_network_wait_fd_timeout(int fd, int write, int64_t timeout, AVIOInterruptCB *int_cb)
This works similarly to ff_network_wait_fd, but waits up to 'timeout' microseconds Uses ff_network_wa...
Definition: network.c:77
uint8_t
miscellaneous OS support macros and functions.
uint16_t ss_family
Definition: network.h:113
static int flags
Definition: log.c:55
int ff_http_match_no_proxy(const char *no_proxy, const char *hostname)
Definition: network.c:320
void ff_network_close(void)
Definition: network.c:115
Callback for checking whether to abort blocking functions.
Definition: avio.h:58
int ff_tls_init(void)
Definition: network.c:30
error code definitions
const AVIOInterruptCB int_cb
Definition: ffmpeg.c:475
const char * ff_gai_strerror(int ecode)
char * ai_canonname
Definition: network.h:141
void ff_freeaddrinfo(struct addrinfo *res)
int ai_addrlen
Definition: network.h:139
#define ff_neterrno()
Definition: network.h:65
struct sockaddr_in in
Definition: network.h:123
int ff_listen_connect(int fd, const struct sockaddr *addr, socklen_t addrlen, int timeout, URLContext *h, int will_try_next)
Connect to a file descriptor and poll for result.
Definition: network.c:251
int ff_socket_nonblock(int socket, int enable)
int64_t ss_align
Definition: network.h:116
int ff_accept(int fd, int timeout, URLContext *h)
Poll for a single connection on the passed file descriptor.
Definition: network.c:221
int ff_listen_bind(int fd, const struct sockaddr *addr, socklen_t addrlen, int timeout, URLContext *h)
Bind to a file descriptor and poll for a connection.
Definition: network.c:239
struct sockaddr_storage storage
Definition: network.h:122
Definition: url.h:38
GLint GLenum type
Definition: opengl_enc.c:105
int ff_listen(int fd, const struct sockaddr *addr, socklen_t addrlen)
Bind to a file descriptor to an address without accepting connections.
Definition: network.c:203
int ai_protocol
Definition: network.h:138
void ff_tls_deinit(void)
Definition: network.c:45
int ai_socktype
Definition: network.h:137
struct addrinfo * ai_next
Definition: network.h:142
int ff_network_init(void)
Definition: network.c:57
char ss_pad2[112]
Definition: network.h:117
int ff_getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res)
int ff_socket(int domain, int type, int protocol)
Definition: network.c:179
int ai_flags
Definition: network.h:135
unbuffered private I/O API
int ff_network_sleep_interruptible(int64_t timeout, AVIOInterruptCB *int_cb)
Waits for up to 'timeout' microseconds.
Definition: network.c:97
struct sockaddr * ai_addr
Definition: network.h:140
int ai_family
Definition: network.h:136