2 * Copyright (c) 2007 The Libav Project
4 * This file is part of Libav.
6 * Libav 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 * Libav 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 Libav; 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
27 #include "libavutil/error.h"
28 #include "os_support.h"
34 #define EPROTONOSUPPORT WSAEPROTONOSUPPORT
35 #define ETIMEDOUT WSAETIMEDOUT
36 #define ECONNREFUSED WSAECONNREFUSED
37 #define EINPROGRESS WSAEINPROGRESS
39 static inline int ff_neterrno(void)
41 int err = WSAGetLastError();
44 return AVERROR(EAGAIN);
46 return AVERROR(EINTR);
51 #include <sys/types.h>
52 #include <sys/socket.h>
53 #include <netinet/in.h>
56 #define ff_neterrno() AVERROR(errno)
60 #include <arpa/inet.h>
67 int ff_socket_nonblock(int socket, int enable);
69 static inline int ff_network_init(void)
73 if (WSAStartup(MAKEWORD(1,1), &wsaData))
79 static inline int ff_network_wait_fd(int fd, int write)
81 int ev = write ? POLLOUT : POLLIN;
82 struct pollfd p = { .fd = fd, .events = ev, .revents = 0 };
84 ret = poll(&p, 1, 100);
85 return ret < 0 ? ff_neterrno() : p.revents & (ev | POLLERR | POLLHUP) ? 0 : AVERROR(EAGAIN);
88 static inline void ff_network_close(void)
95 int ff_inet_aton (const char * str, struct in_addr * add);
97 #if !HAVE_STRUCT_SOCKADDR_STORAGE
98 struct sockaddr_storage {
99 #if HAVE_STRUCT_SOCKADDR_SA_LEN
111 #if !HAVE_STRUCT_ADDRINFO
118 struct sockaddr *ai_addr;
120 struct addrinfo *ai_next;
124 /* getaddrinfo constants */
142 #define AI_CANONNAME 2
145 #ifndef AI_NUMERICHOST
146 #define AI_NUMERICHOST 4
153 #ifndef NI_NUMERICHOST
154 #define NI_NUMERICHOST 2
161 #ifndef NI_NUMERICSERV
162 #define NI_NUMERICSERV 8
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
183 #ifndef INET6_ADDRSTRLEN
184 #define INET6_ADDRSTRLEN INET_ADDRSTRLEN
188 #define IN_MULTICAST(a) ((((uint32_t)(a)) & 0xf0000000) == 0xe0000000)
190 #ifndef IN6_IS_ADDR_MULTICAST
191 #define IN6_IS_ADDR_MULTICAST(a) (((uint8_t *) (a))[0] == 0xff)
194 static inline int ff_is_multicast_address(struct sockaddr *addr)
196 if (addr->sa_family == AF_INET) {
197 return IN_MULTICAST(ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr));
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);
208 #endif /* AVFORMAT_NETWORK_H */