2 * Copyright (c) 2007 The FFmpeg Project
4 * This file is part of FFmpeg.
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.
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.
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
21 #ifndef AVFORMAT_NETWORK_H
22 #define AVFORMAT_NETWORK_H
25 #include "os_support.h"
31 #define EPROTONOSUPPORT WSAEPROTONOSUPPORT
32 #define ETIMEDOUT WSAETIMEDOUT
33 #define ECONNREFUSED WSAECONNREFUSED
34 #define EINPROGRESS WSAEINPROGRESS
36 static inline int ff_neterrno(void)
38 int err = WSAGetLastError();
41 return AVERROR(EAGAIN);
43 return AVERROR(EINTR);
48 #include <sys/types.h>
49 #include <sys/socket.h>
50 #include <netinet/in.h>
53 #define ff_neterrno() AVERROR(errno)
57 #include <arpa/inet.h>
64 int ff_socket_nonblock(int socket, int enable);
66 static inline int ff_network_init(void)
70 if (WSAStartup(MAKEWORD(1,1), &wsaData))
76 static inline int ff_network_wait_fd(int fd, int write)
78 int ev = write ? POLLOUT : POLLIN;
79 struct pollfd p = { .fd = fd, .events = ev, .revents = 0 };
81 ret = poll(&p, 1, 100);
82 return ret < 0 ? ff_neterrno() : p.revents & (ev | POLLERR | POLLHUP) ? 0 : AVERROR(EAGAIN);
85 static inline void ff_network_close(void)
92 int ff_inet_aton (const char * str, struct in_addr * add);
94 #if !HAVE_STRUCT_SOCKADDR_STORAGE
95 struct sockaddr_storage {
96 #if HAVE_STRUCT_SOCKADDR_SA_LEN
108 #if !HAVE_STRUCT_ADDRINFO
115 struct sockaddr *ai_addr;
117 struct addrinfo *ai_next;
121 /* getaddrinfo constants */
139 #define AI_CANONNAME 2
142 #ifndef AI_NUMERICHOST
143 #define AI_NUMERICHOST 4
150 #ifndef NI_NUMERICHOST
151 #define NI_NUMERICHOST 2
158 #ifndef NI_NUMERICSERV
159 #define NI_NUMERICSERV 8
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
180 #ifndef INET6_ADDRSTRLEN
181 #define INET6_ADDRSTRLEN INET_ADDRSTRLEN
185 #define IN_MULTICAST(a) ((((uint32_t)(a)) & 0xf0000000) == 0xe0000000)
187 #ifndef IN6_IS_ADDR_MULTICAST
188 #define IN6_IS_ADDR_MULTICAST(a) (((uint8_t *) (a))[0] == 0xff)
191 static inline int ff_is_multicast_address(struct sockaddr *addr)
193 if (addr->sa_family == AF_INET) {
194 return IN_MULTICAST(ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr));
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);
205 #endif /* AVFORMAT_NETWORK_H */