OSDN Git Service

Merge remote-tracking branch 'qatar/master'
[coroid/ffmpeg_saccubus.git] / 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
26 #include "config.h"
27 #include "libavutil/error.h"
28 #include "os_support.h"
29
30 #if HAVE_WINSOCK2_H
31 #include <winsock2.h>
32 #include <ws2tcpip.h>
33
34 #define EPROTONOSUPPORT WSAEPROTONOSUPPORT
35 #define ETIMEDOUT       WSAETIMEDOUT
36 #define ECONNREFUSED    WSAECONNREFUSED
37 #define EINPROGRESS     WSAEINPROGRESS
38
39 static inline int ff_neterrno(void)
40 {
41     int err = WSAGetLastError();
42     switch (err) {
43     case WSAEWOULDBLOCK:
44         return AVERROR(EAGAIN);
45     case WSAEINTR:
46         return AVERROR(EINTR);
47     }
48     return -err;
49 }
50 #else
51 #include <sys/types.h>
52 #include <sys/socket.h>
53 #include <netinet/in.h>
54 #include <netdb.h>
55
56 #define ff_neterrno() AVERROR(errno)
57 #endif
58
59 #if HAVE_ARPA_INET_H
60 #include <arpa/inet.h>
61 #endif
62
63 #if HAVE_POLL_H
64 #include <poll.h>
65 #endif
66
67 int ff_socket_nonblock(int socket, int enable);
68
69 static inline int ff_network_init(void)
70 {
71 #if HAVE_WINSOCK2_H
72     WSADATA wsaData;
73     if (WSAStartup(MAKEWORD(1,1), &wsaData))
74         return 0;
75 #endif
76     return 1;
77 }
78
79 static inline int ff_network_wait_fd(int fd, int write)
80 {
81     int ev = write ? POLLOUT : POLLIN;
82     struct pollfd p = { .fd = fd, .events = ev, .revents = 0 };
83     int ret;
84     ret = poll(&p, 1, 100);
85     return ret < 0 ? ff_neterrno() : p.revents & (ev | POLLERR | POLLHUP) ? 0 : AVERROR(EAGAIN);
86 }
87
88 static inline void ff_network_close(void)
89 {
90 #if HAVE_WINSOCK2_H
91     WSACleanup();
92 #endif
93 }
94
95 int ff_inet_aton (const char * str, struct in_addr * add);
96
97 #if !HAVE_STRUCT_SOCKADDR_STORAGE
98 struct sockaddr_storage {
99 #if HAVE_STRUCT_SOCKADDR_SA_LEN
100     uint8_t ss_len;
101     uint8_t ss_family;
102 #else
103     uint16_t ss_family;
104 #endif
105     char ss_pad1[6];
106     int64_t ss_align;
107     char ss_pad2[112];
108 };
109 #endif
110
111 #if !HAVE_STRUCT_ADDRINFO
112 struct addrinfo {
113     int ai_flags;
114     int ai_family;
115     int ai_socktype;
116     int ai_protocol;
117     int ai_addrlen;
118     struct sockaddr *ai_addr;
119     char *ai_canonname;
120     struct addrinfo *ai_next;
121 };
122 #endif
123
124 /* getaddrinfo constants */
125 #ifndef EAI_FAIL
126 #define EAI_FAIL 4
127 #endif
128
129 #ifndef EAI_FAMILY
130 #define EAI_FAMILY 5
131 #endif
132
133 #ifndef EAI_NONAME
134 #define EAI_NONAME 8
135 #endif
136
137 #ifndef AI_PASSIVE
138 #define AI_PASSIVE 1
139 #endif
140
141 #ifndef AI_CANONNAME
142 #define AI_CANONNAME 2
143 #endif
144
145 #ifndef AI_NUMERICHOST
146 #define AI_NUMERICHOST 4
147 #endif
148
149 #ifndef NI_NOFQDN
150 #define NI_NOFQDN 1
151 #endif
152
153 #ifndef NI_NUMERICHOST
154 #define NI_NUMERICHOST 2
155 #endif
156
157 #ifndef NI_NAMERQD
158 #define NI_NAMERQD 4
159 #endif
160
161 #ifndef NI_NUMERICSERV
162 #define NI_NUMERICSERV 8
163 #endif
164
165 #ifndef NI_DGRAM
166 #define NI_DGRAM 16
167 #endif
168
169 #if !HAVE_GETADDRINFO
170 int ff_getaddrinfo(const char *node, const char *service,
171                    const struct addrinfo *hints, struct addrinfo **res);
172 void ff_freeaddrinfo(struct addrinfo *res);
173 int ff_getnameinfo(const struct sockaddr *sa, int salen,
174                    char *host, int hostlen,
175                    char *serv, int servlen, int flags);
176 const char *ff_gai_strerror(int ecode);
177 #define getaddrinfo ff_getaddrinfo
178 #define freeaddrinfo ff_freeaddrinfo
179 #define getnameinfo ff_getnameinfo
180 #define gai_strerror ff_gai_strerror
181 #endif
182
183 #ifndef INET6_ADDRSTRLEN
184 #define INET6_ADDRSTRLEN INET_ADDRSTRLEN
185 #endif
186
187 #ifndef IN_MULTICAST
188 #define IN_MULTICAST(a) ((((uint32_t)(a)) & 0xf0000000) == 0xe0000000)
189 #endif
190 #ifndef IN6_IS_ADDR_MULTICAST
191 #define IN6_IS_ADDR_MULTICAST(a) (((uint8_t *) (a))[0] == 0xff)
192 #endif
193
194 static inline int ff_is_multicast_address(struct sockaddr *addr)
195 {
196     if (addr->sa_family == AF_INET) {
197         return IN_MULTICAST(ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr));
198     }
199 #if HAVE_STRUCT_SOCKADDR_IN6
200     if (addr->sa_family == AF_INET6) {
201         return IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)addr)->sin6_addr);
202     }
203 #endif
204
205     return 0;
206 }
207
208 #endif /* AVFORMAT_NETWORK_H */