You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

206 lines
4.4KB

  1. /*
  2. * Copyright (c) 2007 The Libav Project
  3. *
  4. * This file is part of Libav.
  5. *
  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.
  10. *
  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.
  15. *
  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
  19. */
  20. #ifndef AVFORMAT_NETWORK_H
  21. #define AVFORMAT_NETWORK_H
  22. #include "config.h"
  23. #include "os_support.h"
  24. #if HAVE_WINSOCK2_H
  25. #include <winsock2.h>
  26. #include <ws2tcpip.h>
  27. #define EPROTONOSUPPORT WSAEPROTONOSUPPORT
  28. #define ETIMEDOUT WSAETIMEDOUT
  29. #define ECONNREFUSED WSAECONNREFUSED
  30. #define EINPROGRESS WSAEINPROGRESS
  31. static inline int ff_neterrno(void)
  32. {
  33. int err = WSAGetLastError();
  34. switch (err) {
  35. case WSAEWOULDBLOCK:
  36. return AVERROR(EAGAIN);
  37. case WSAEINTR:
  38. return AVERROR(EINTR);
  39. }
  40. return -err;
  41. }
  42. #else
  43. #include <sys/types.h>
  44. #include <sys/socket.h>
  45. #include <netinet/in.h>
  46. #include <netdb.h>
  47. #define ff_neterrno() AVERROR(errno)
  48. #endif
  49. #if HAVE_ARPA_INET_H
  50. #include <arpa/inet.h>
  51. #endif
  52. #if HAVE_POLL_H
  53. #include <poll.h>
  54. #endif
  55. int ff_socket_nonblock(int socket, int enable);
  56. static inline int ff_network_init(void)
  57. {
  58. #if HAVE_WINSOCK2_H
  59. WSADATA wsaData;
  60. if (WSAStartup(MAKEWORD(1,1), &wsaData))
  61. return 0;
  62. #endif
  63. return 1;
  64. }
  65. static inline int ff_network_wait_fd(int fd, int write)
  66. {
  67. int ev = write ? POLLOUT : POLLIN;
  68. struct pollfd p = { .fd = fd, .events = ev, .revents = 0 };
  69. int ret;
  70. ret = poll(&p, 1, 100);
  71. return ret < 0 ? ff_neterrno() : p.revents & (ev | POLLERR | POLLHUP) ? 0 : AVERROR(EAGAIN);
  72. }
  73. static inline void ff_network_close(void)
  74. {
  75. #if HAVE_WINSOCK2_H
  76. WSACleanup();
  77. #endif
  78. }
  79. int ff_inet_aton (const char * str, struct in_addr * add);
  80. #if !HAVE_STRUCT_SOCKADDR_STORAGE
  81. struct sockaddr_storage {
  82. #if HAVE_STRUCT_SOCKADDR_SA_LEN
  83. uint8_t ss_len;
  84. uint8_t ss_family;
  85. #else
  86. uint16_t ss_family;
  87. #endif
  88. char ss_pad1[6];
  89. int64_t ss_align;
  90. char ss_pad2[112];
  91. };
  92. #endif
  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. /* getaddrinfo constants */
  106. #ifndef EAI_FAIL
  107. #define EAI_FAIL 4
  108. #endif
  109. #ifndef EAI_FAMILY
  110. #define EAI_FAMILY 5
  111. #endif
  112. #ifndef EAI_NONAME
  113. #define EAI_NONAME 8
  114. #endif
  115. #ifndef AI_PASSIVE
  116. #define AI_PASSIVE 1
  117. #endif
  118. #ifndef AI_CANONNAME
  119. #define AI_CANONNAME 2
  120. #endif
  121. #ifndef AI_NUMERICHOST
  122. #define AI_NUMERICHOST 4
  123. #endif
  124. #ifndef NI_NOFQDN
  125. #define NI_NOFQDN 1
  126. #endif
  127. #ifndef NI_NUMERICHOST
  128. #define NI_NUMERICHOST 2
  129. #endif
  130. #ifndef NI_NAMERQD
  131. #define NI_NAMERQD 4
  132. #endif
  133. #ifndef NI_NUMERICSERV
  134. #define NI_NUMERICSERV 8
  135. #endif
  136. #ifndef NI_DGRAM
  137. #define NI_DGRAM 16
  138. #endif
  139. #if !HAVE_GETADDRINFO
  140. int ff_getaddrinfo(const char *node, const char *service,
  141. const struct addrinfo *hints, struct addrinfo **res);
  142. void ff_freeaddrinfo(struct addrinfo *res);
  143. int ff_getnameinfo(const struct sockaddr *sa, int salen,
  144. char *host, int hostlen,
  145. char *serv, int servlen, int flags);
  146. const char *ff_gai_strerror(int ecode);
  147. #define getaddrinfo ff_getaddrinfo
  148. #define freeaddrinfo ff_freeaddrinfo
  149. #define getnameinfo ff_getnameinfo
  150. #define gai_strerror ff_gai_strerror
  151. #endif
  152. #ifndef INET6_ADDRSTRLEN
  153. #define INET6_ADDRSTRLEN INET_ADDRSTRLEN
  154. #endif
  155. #ifndef IN_MULTICAST
  156. #define IN_MULTICAST(a) ((((uint32_t)(a)) & 0xf0000000) == 0xe0000000)
  157. #endif
  158. #ifndef IN6_IS_ADDR_MULTICAST
  159. #define IN6_IS_ADDR_MULTICAST(a) (((uint8_t *) (a))[0] == 0xff)
  160. #endif
  161. static inline int ff_is_multicast_address(struct sockaddr *addr)
  162. {
  163. if (addr->sa_family == AF_INET) {
  164. return IN_MULTICAST(ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr));
  165. }
  166. #if HAVE_STRUCT_SOCKADDR_IN6
  167. if (addr->sa_family == AF_INET6) {
  168. return IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)addr)->sin6_addr);
  169. }
  170. #endif
  171. return 0;
  172. }
  173. #endif /* AVFORMAT_NETWORK_H */