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