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 
32 #if HAVE_UNISTD_H
33 #include <unistd.h>
34 #endif
35 
36 #if HAVE_WINSOCK2_H
37 #include <winsock2.h>
38 #include <ws2tcpip.h>
39 
40 #ifndef EPROTONOSUPPORT
41 #define EPROTONOSUPPORT WSAEPROTONOSUPPORT
42 #endif
43 #ifndef ETIMEDOUT
44 #define ETIMEDOUT WSAETIMEDOUT
45 #endif
46 #ifndef ECONNREFUSED
47 #define ECONNREFUSED WSAECONNREFUSED
48 #endif
49 #ifndef EINPROGRESS
50 #define EINPROGRESS WSAEINPROGRESS
51 #endif
52 
53 #define getsockopt(a, b, c, d, e) getsockopt(a, b, c, (char*) d, e)
54 #define setsockopt(a, b, c, d, e) setsockopt(a, b, c, (const char*) d, e)
55 
56 int ff_neterrno(void);
57 #else
58 #include <sys/types.h>
59 #include <sys/socket.h>
60 #include <netinet/in.h>
61 #include <netdb.h>
62 
63 #define ff_neterrno() AVERROR(errno)
64 #endif
65 
66 #if HAVE_ARPA_INET_H
67 #include <arpa/inet.h>
68 #endif
69 
70 #if HAVE_POLL_H
71 #include <poll.h>
72 #endif
73 
74 int ff_socket_nonblock(int socket, int enable);
75 
77 int ff_network_init(void);
78 void ff_network_close(void);
79 
80 void ff_tls_init(void);
81 void ff_tls_deinit(void);
82 
83 int ff_network_wait_fd(int fd, int write);
84 
85 /**
86  * This works similarly to ff_network_wait_fd, but waits up to 'timeout' microseconds
87  * Uses ff_network_wait_fd in a loop
88  *
89  * @fd Socket descriptor
90  * @write Set 1 to wait for socket able to be read, 0 to be written
91  * @timeout Timeout interval, in microseconds. Actual precision is 100000 mcs, due to ff_network_wait_fd usage
92  * @param int_cb Interrupt callback, is checked after each ff_network_wait_fd call
93  * @return 0 if data can be read/written, AVERROR(ETIMEDOUT) if timeout expired, or negative error code
94  */
95 int ff_network_wait_fd_timeout(int fd, int write, int64_t timeout, AVIOInterruptCB *int_cb);
96 
97 int ff_inet_aton (const char * str, struct in_addr * add);
98 
99 #if !HAVE_STRUCT_SOCKADDR_STORAGE
101 #if HAVE_STRUCT_SOCKADDR_SA_LEN
102  uint8_t ss_len;
104 #else
105  uint16_t ss_family;
106 #endif
107  char ss_pad1[6];
108  int64_t ss_align;
109  char ss_pad2[112];
110 };
111 #endif
112 
113 #if !HAVE_STRUCT_ADDRINFO
114 struct addrinfo {
115  int ai_flags;
120  struct sockaddr *ai_addr;
122  struct addrinfo *ai_next;
123 };
124 #endif
125 
126 /* getaddrinfo constants */
127 #ifndef EAI_AGAIN
128 #define EAI_AGAIN 2
129 #endif
130 #ifndef EAI_BADFLAGS
131 #define EAI_BADFLAGS 3
132 #endif
133 #ifndef EAI_FAIL
134 #define EAI_FAIL 4
135 #endif
136 #ifndef EAI_FAMILY
137 #define EAI_FAMILY 5
138 #endif
139 #ifndef EAI_MEMORY
140 #define EAI_MEMORY 6
141 #endif
142 #ifndef EAI_NODATA
143 #define EAI_NODATA 7
144 #endif
145 #ifndef EAI_NONAME
146 #define EAI_NONAME 8
147 #endif
148 #ifndef EAI_SERVICE
149 #define EAI_SERVICE 9
150 #endif
151 #ifndef EAI_SOCKTYPE
152 #define EAI_SOCKTYPE 10
153 #endif
154 
155 #ifndef AI_PASSIVE
156 #define AI_PASSIVE 1
157 #endif
158 
159 #ifndef AI_CANONNAME
160 #define AI_CANONNAME 2
161 #endif
162 
163 #ifndef AI_NUMERICHOST
164 #define AI_NUMERICHOST 4
165 #endif
166 
167 #ifndef NI_NOFQDN
168 #define NI_NOFQDN 1
169 #endif
170 
171 #ifndef NI_NUMERICHOST
172 #define NI_NUMERICHOST 2
173 #endif
174 
175 #ifndef NI_NAMERQD
176 #define NI_NAMERQD 4
177 #endif
178 
179 #ifndef NI_NUMERICSERV
180 #define NI_NUMERICSERV 8
181 #endif
182 
183 #ifndef NI_DGRAM
184 #define NI_DGRAM 16
185 #endif
186 
187 #if !HAVE_GETADDRINFO
188 int ff_getaddrinfo(const char *node, const char *service,
189  const struct addrinfo *hints, struct addrinfo **res);
190 void ff_freeaddrinfo(struct addrinfo *res);
191 int ff_getnameinfo(const struct sockaddr *sa, int salen,
192  char *host, int hostlen,
193  char *serv, int servlen, int flags);
194 #define getaddrinfo ff_getaddrinfo
195 #define freeaddrinfo ff_freeaddrinfo
196 #define getnameinfo ff_getnameinfo
197 #endif
198 #if !HAVE_GETADDRINFO || HAVE_WINSOCK2_H
199 const char *ff_gai_strerror(int ecode);
200 #undef gai_strerror
201 #define gai_strerror ff_gai_strerror
202 #endif
203 
204 #ifndef INADDR_LOOPBACK
205 #define INADDR_LOOPBACK 0x7f000001
206 #endif
207 
208 #ifndef INET_ADDRSTRLEN
209 #define INET_ADDRSTRLEN 16
210 #endif
211 
212 #ifndef INET6_ADDRSTRLEN
213 #define INET6_ADDRSTRLEN INET_ADDRSTRLEN
214 #endif
215 
216 #ifndef IN_MULTICAST
217 #define IN_MULTICAST(a) ((((uint32_t)(a)) & 0xf0000000) == 0xe0000000)
218 #endif
219 #ifndef IN6_IS_ADDR_MULTICAST
220 #define IN6_IS_ADDR_MULTICAST(a) (((uint8_t *) (a))[0] == 0xff)
221 #endif
222 
223 int ff_is_multicast_address(struct sockaddr *addr);
224 
225 #endif /* AVFORMAT_NETWORK_H */
Buffered I/O operations.
char ss_pad1[6]
Definition: network.h:107
int ff_network_wait_fd(int fd, int write)
Definition: network.c:144
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:202
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 &#39;timeout&#39; microseconds Uses ff_network_wa...
Definition: network.c:153
uint8_t
miscellaneous OS support macros and functions.
uint16_t ss_family
Definition: network.h:105
void ff_tls_init(void)
Definition: network.c:69
void ff_network_close(void)
Definition: network.c:173
Callback for checking whether to abort blocking functions.
Definition: avio.h:51
error code definitions
const AVIOInterruptCB int_cb
Definition: ffmpeg.c:422
const char * ff_gai_strerror(int ecode)
char * ai_canonname
Definition: network.h:121
void ff_freeaddrinfo(struct addrinfo *res)
int ai_addrlen
Definition: network.h:119
int ff_inet_aton(const char *str, struct in_addr *add)
#define ff_neterrno()
Definition: network.h:63
int ff_socket_nonblock(int socket, int enable)
int64_t ss_align
Definition: network.h:108
int ai_protocol
Definition: network.h:118
void ff_tls_deinit(void)
Definition: network.c:101
int ai_socktype
Definition: network.h:117
static int flags
Definition: cpu.c:23
int ff_network_inited_globally
Definition: network.c:124
struct addrinfo * ai_next
Definition: network.h:122
int ff_network_init(void)
Definition: network.c:126
char ss_pad2[112]
Definition: network.h:109
int ff_getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res)
int ai_flags
Definition: network.h:115
struct sockaddr * ai_addr
Definition: network.h:120
int ai_family
Definition: network.h:116