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