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