Libav 0.7.1
|
00001 /* 00002 * Copyright (c) 2007 The Libav Project 00003 * 00004 * This file is part of Libav. 00005 * 00006 * Libav is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU Lesser General Public 00008 * License as published by the Free Software Foundation; either 00009 * version 2.1 of the License, or (at your option) any later version. 00010 * 00011 * Libav is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 * Lesser General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Lesser General Public 00017 * License along with Libav; if not, write to the Free Software 00018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00019 */ 00020 00021 #ifndef AVFORMAT_NETWORK_H 00022 #define AVFORMAT_NETWORK_H 00023 00024 #include <errno.h> 00025 00026 #include "config.h" 00027 #include "libavutil/error.h" 00028 #include "os_support.h" 00029 00030 #if HAVE_WINSOCK2_H 00031 #include <winsock2.h> 00032 #include <ws2tcpip.h> 00033 00034 #define EPROTONOSUPPORT WSAEPROTONOSUPPORT 00035 #define ETIMEDOUT WSAETIMEDOUT 00036 #define ECONNREFUSED WSAECONNREFUSED 00037 #define EINPROGRESS WSAEINPROGRESS 00038 00039 static inline int ff_neterrno(void) 00040 { 00041 int err = WSAGetLastError(); 00042 switch (err) { 00043 case WSAEWOULDBLOCK: 00044 return AVERROR(EAGAIN); 00045 case WSAEINTR: 00046 return AVERROR(EINTR); 00047 } 00048 return -err; 00049 } 00050 #else 00051 #include <sys/types.h> 00052 #include <sys/socket.h> 00053 #include <netinet/in.h> 00054 #include <netdb.h> 00055 00056 #define ff_neterrno() AVERROR(errno) 00057 #endif 00058 00059 #if HAVE_ARPA_INET_H 00060 #include <arpa/inet.h> 00061 #endif 00062 00063 #if HAVE_POLL_H 00064 #include <poll.h> 00065 #endif 00066 00067 int ff_socket_nonblock(int socket, int enable); 00068 00069 static inline int ff_network_init(void) 00070 { 00071 #if HAVE_WINSOCK2_H 00072 WSADATA wsaData; 00073 if (WSAStartup(MAKEWORD(1,1), &wsaData)) 00074 return 0; 00075 #endif 00076 return 1; 00077 } 00078 00079 static inline int ff_network_wait_fd(int fd, int write) 00080 { 00081 int ev = write ? POLLOUT : POLLIN; 00082 struct pollfd p = { .fd = fd, .events = ev, .revents = 0 }; 00083 int ret; 00084 ret = poll(&p, 1, 100); 00085 return ret < 0 ? ff_neterrno() : p.revents & (ev | POLLERR | POLLHUP) ? 0 : AVERROR(EAGAIN); 00086 } 00087 00088 static inline void ff_network_close(void) 00089 { 00090 #if HAVE_WINSOCK2_H 00091 WSACleanup(); 00092 #endif 00093 } 00094 00095 int ff_inet_aton (const char * str, struct in_addr * add); 00096 00097 #if !HAVE_STRUCT_SOCKADDR_STORAGE 00098 struct sockaddr_storage { 00099 #if HAVE_STRUCT_SOCKADDR_SA_LEN 00100 uint8_t ss_len; 00101 uint8_t ss_family; 00102 #else 00103 uint16_t ss_family; 00104 #endif 00105 char ss_pad1[6]; 00106 int64_t ss_align; 00107 char ss_pad2[112]; 00108 }; 00109 #endif 00110 00111 #if !HAVE_STRUCT_ADDRINFO 00112 struct addrinfo { 00113 int ai_flags; 00114 int ai_family; 00115 int ai_socktype; 00116 int ai_protocol; 00117 int ai_addrlen; 00118 struct sockaddr *ai_addr; 00119 char *ai_canonname; 00120 struct addrinfo *ai_next; 00121 }; 00122 #endif 00123 00124 /* getaddrinfo constants */ 00125 #ifndef EAI_FAIL 00126 #define EAI_FAIL 4 00127 #endif 00128 00129 #ifndef EAI_FAMILY 00130 #define EAI_FAMILY 5 00131 #endif 00132 00133 #ifndef EAI_NONAME 00134 #define EAI_NONAME 8 00135 #endif 00136 00137 #ifndef AI_PASSIVE 00138 #define AI_PASSIVE 1 00139 #endif 00140 00141 #ifndef AI_CANONNAME 00142 #define AI_CANONNAME 2 00143 #endif 00144 00145 #ifndef AI_NUMERICHOST 00146 #define AI_NUMERICHOST 4 00147 #endif 00148 00149 #ifndef NI_NOFQDN 00150 #define NI_NOFQDN 1 00151 #endif 00152 00153 #ifndef NI_NUMERICHOST 00154 #define NI_NUMERICHOST 2 00155 #endif 00156 00157 #ifndef NI_NAMERQD 00158 #define NI_NAMERQD 4 00159 #endif 00160 00161 #ifndef NI_NUMERICSERV 00162 #define NI_NUMERICSERV 8 00163 #endif 00164 00165 #ifndef NI_DGRAM 00166 #define NI_DGRAM 16 00167 #endif 00168 00169 #if !HAVE_GETADDRINFO 00170 int ff_getaddrinfo(const char *node, const char *service, 00171 const struct addrinfo *hints, struct addrinfo **res); 00172 void ff_freeaddrinfo(struct addrinfo *res); 00173 int ff_getnameinfo(const struct sockaddr *sa, int salen, 00174 char *host, int hostlen, 00175 char *serv, int servlen, int flags); 00176 const char *ff_gai_strerror(int ecode); 00177 #define getaddrinfo ff_getaddrinfo 00178 #define freeaddrinfo ff_freeaddrinfo 00179 #define getnameinfo ff_getnameinfo 00180 #define gai_strerror ff_gai_strerror 00181 #endif 00182 00183 #ifndef INET6_ADDRSTRLEN 00184 #define INET6_ADDRSTRLEN INET_ADDRSTRLEN 00185 #endif 00186 00187 #ifndef IN_MULTICAST 00188 #define IN_MULTICAST(a) ((((uint32_t)(a)) & 0xf0000000) == 0xe0000000) 00189 #endif 00190 #ifndef IN6_IS_ADDR_MULTICAST 00191 #define IN6_IS_ADDR_MULTICAST(a) (((uint8_t *) (a))[0] == 0xff) 00192 #endif 00193 00194 static inline int ff_is_multicast_address(struct sockaddr *addr) 00195 { 00196 if (addr->sa_family == AF_INET) { 00197 return IN_MULTICAST(ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr)); 00198 } 00199 #if HAVE_STRUCT_SOCKADDR_IN6 00200 if (addr->sa_family == AF_INET6) { 00201 return IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)addr)->sin6_addr); 00202 } 00203 #endif 00204 00205 return 0; 00206 } 00207 00208 #endif /* AVFORMAT_NETWORK_H */