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 <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 
78 int ff_network_init(void);
79 void ff_network_close(void);
80 
81 void 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
102 #if HAVE_STRUCT_SOCKADDR_SA_LEN
103  uint8_t ss_len;
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 #ifndef MSG_NOSIGNAL
115 #define MSG_NOSIGNAL 0
116 #endif
117 
118 #if !HAVE_STRUCT_ADDRINFO
119 struct addrinfo {
120  int ai_flags;
125  struct sockaddr *ai_addr;
127  struct addrinfo *ai_next;
128 };
129 #endif /* !HAVE_STRUCT_ADDRINFO */
130 
131 /* getaddrinfo constants */
132 #ifndef EAI_AGAIN
133 #define EAI_AGAIN 2
134 #endif
135 #ifndef EAI_BADFLAGS
136 #define EAI_BADFLAGS 3
137 #endif
138 #ifndef EAI_FAIL
139 #define EAI_FAIL 4
140 #endif
141 #ifndef EAI_FAMILY
142 #define EAI_FAMILY 5
143 #endif
144 #ifndef EAI_MEMORY
145 #define EAI_MEMORY 6
146 #endif
147 #ifndef EAI_NODATA
148 #define EAI_NODATA 7
149 #endif
150 #ifndef EAI_NONAME
151 #define EAI_NONAME 8
152 #endif
153 #ifndef EAI_SERVICE
154 #define EAI_SERVICE 9
155 #endif
156 #ifndef EAI_SOCKTYPE
157 #define EAI_SOCKTYPE 10
158 #endif
159 
160 #ifndef AI_PASSIVE
161 #define AI_PASSIVE 1
162 #endif
163 
164 #ifndef AI_CANONNAME
165 #define AI_CANONNAME 2
166 #endif
167 
168 #ifndef AI_NUMERICHOST
169 #define AI_NUMERICHOST 4
170 #endif
171 
172 #ifndef NI_NOFQDN
173 #define NI_NOFQDN 1
174 #endif
175 
176 #ifndef NI_NUMERICHOST
177 #define NI_NUMERICHOST 2
178 #endif
179 
180 #ifndef NI_NAMERQD
181 #define NI_NAMERQD 4
182 #endif
183 
184 #ifndef NI_NUMERICSERV
185 #define NI_NUMERICSERV 8
186 #endif
187 
188 #ifndef NI_DGRAM
189 #define NI_DGRAM 16
190 #endif
191 
192 #if !HAVE_GETADDRINFO
193 int ff_getaddrinfo(const char *node, const char *service,
194  const struct addrinfo *hints, struct addrinfo **res);
195 void ff_freeaddrinfo(struct addrinfo *res);
196 int ff_getnameinfo(const struct sockaddr *sa, int salen,
197  char *host, int hostlen,
198  char *serv, int servlen, int flags);
199 #define getaddrinfo ff_getaddrinfo
200 #define freeaddrinfo ff_freeaddrinfo
201 #define getnameinfo ff_getnameinfo
202 #endif /* !HAVE_GETADDRINFO */
203 
204 #if !HAVE_GETADDRINFO || HAVE_WINSOCK2_H
205 const char *ff_gai_strerror(int ecode);
206 #undef gai_strerror
207 #define gai_strerror ff_gai_strerror
208 #endif /* !HAVE_GETADDRINFO || HAVE_WINSOCK2_H */
209 
210 #ifndef INADDR_LOOPBACK
211 #define INADDR_LOOPBACK 0x7f000001
212 #endif
213 
214 #ifndef INET_ADDRSTRLEN
215 #define INET_ADDRSTRLEN 16
216 #endif
217 
218 #ifndef INET6_ADDRSTRLEN
219 #define INET6_ADDRSTRLEN INET_ADDRSTRLEN
220 #endif
221 
222 #ifndef IN_MULTICAST
223 #define IN_MULTICAST(a) ((((uint32_t)(a)) & 0xf0000000) == 0xe0000000)
224 #endif
225 #ifndef IN6_IS_ADDR_MULTICAST
226 #define IN6_IS_ADDR_MULTICAST(a) (((uint8_t *) (a))[0] == 0xff)
227 #endif
228 
229 int ff_is_multicast_address(struct sockaddr *addr);
230 
231 #define POLLING_TIME 100 /// Time in milliseconds between interrupt check
232 
233 /**
234  * Bind to a file descriptor and poll for a connection.
235  *
236  * @param fd First argument of bind().
237  * @param addr Second argument of bind().
238  * @param addrlen Third argument of bind().
239  * @param timeout Polling timeout in milliseconds.
240  * @param h URLContext providing interrupt check
241  * callback and logging context.
242  * @return A non-blocking file descriptor on success
243  * or an AVERROR on failure.
244  */
245 int ff_listen_bind(int fd, const struct sockaddr *addr,
246  socklen_t addrlen, int timeout,
247  URLContext *h);
248 
249 /**
250  * Connect to a file descriptor and poll for result.
251  *
252  * @param fd First argument of connect(),
253  * will be set as non-blocking.
254  * @param addr Second argument of connect().
255  * @param addrlen Third argument of connect().
256  * @param timeout Polling timeout in milliseconds.
257  * @param h URLContext providing interrupt check
258  * callback and logging context.
259  * @param will_try_next Whether the caller will try to connect to another
260  * address for the same host name, affecting the form of
261  * logged errors.
262  * @return 0 on success, AVERROR on failure.
263  */
264 int ff_listen_connect(int fd, const struct sockaddr *addr,
265  socklen_t addrlen, int timeout,
266  URLContext *h, int will_try_next);
267 
268 int ff_http_match_no_proxy(const char *no_proxy, const char *hostname);
269 
270 int ff_socket(int domain, int type, int protocol);
271 
272 #endif /* AVFORMAT_NETWORK_H */