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.

205 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() {
  32. int err = WSAGetLastError();
  33. switch (err) {
  34. case WSAEWOULDBLOCK:
  35. return AVERROR(EAGAIN);
  36. case WSAEINTR:
  37. return AVERROR(EINTR);
  38. }
  39. return -err;
  40. }
  41. #else
  42. #include <sys/types.h>
  43. #include <sys/socket.h>
  44. #include <netinet/in.h>
  45. #include <netdb.h>
  46. #define ff_neterrno() AVERROR(errno)
  47. #endif
  48. #if HAVE_ARPA_INET_H
  49. #include <arpa/inet.h>
  50. #endif
  51. #if HAVE_POLL_H
  52. #include <poll.h>
  53. #endif
  54. int ff_socket_nonblock(int socket, int enable);
  55. static inline int ff_network_init(void)
  56. {
  57. #if HAVE_WINSOCK2_H
  58. WSADATA wsaData;
  59. if (WSAStartup(MAKEWORD(1,1), &wsaData))
  60. return 0;
  61. #endif
  62. return 1;
  63. }
  64. static inline int ff_network_wait_fd(int fd, int write)
  65. {
  66. int ev = write ? POLLOUT : POLLIN;
  67. struct pollfd p = { .fd = fd, .events = ev, .revents = 0 };
  68. int ret;
  69. ret = poll(&p, 1, 100);
  70. return ret < 0 ? ff_neterrno() : p.revents & (ev | POLLERR | POLLHUP) ? 0 : AVERROR(EAGAIN);
  71. }
  72. static inline void ff_network_close(void)
  73. {
  74. #if HAVE_WINSOCK2_H
  75. WSACleanup();
  76. #endif
  77. }
  78. int ff_inet_aton (const char * str, struct in_addr * add);
  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. #if !HAVE_STRUCT_ADDRINFO
  93. struct addrinfo {
  94. int ai_flags;
  95. int ai_family;
  96. int ai_socktype;
  97. int ai_protocol;
  98. int ai_addrlen;
  99. struct sockaddr *ai_addr;
  100. char *ai_canonname;
  101. struct addrinfo *ai_next;
  102. };
  103. #endif
  104. /* getaddrinfo constants */
  105. #ifndef EAI_FAIL
  106. #define EAI_FAIL 4
  107. #endif
  108. #ifndef EAI_FAMILY
  109. #define EAI_FAMILY 5
  110. #endif
  111. #ifndef EAI_NONAME
  112. #define EAI_NONAME 8
  113. #endif
  114. #ifndef AI_PASSIVE
  115. #define AI_PASSIVE 1
  116. #endif
  117. #ifndef AI_CANONNAME
  118. #define AI_CANONNAME 2
  119. #endif
  120. #ifndef AI_NUMERICHOST
  121. #define AI_NUMERICHOST 4
  122. #endif
  123. #ifndef NI_NOFQDN
  124. #define NI_NOFQDN 1
  125. #endif
  126. #ifndef NI_NUMERICHOST
  127. #define NI_NUMERICHOST 2
  128. #endif
  129. #ifndef NI_NAMERQD
  130. #define NI_NAMERQD 4
  131. #endif
  132. #ifndef NI_NUMERICSERV
  133. #define NI_NUMERICSERV 8
  134. #endif
  135. #ifndef NI_DGRAM
  136. #define NI_DGRAM 16
  137. #endif
  138. #if !HAVE_GETADDRINFO
  139. int ff_getaddrinfo(const char *node, const char *service,
  140. const struct addrinfo *hints, struct addrinfo **res);
  141. void ff_freeaddrinfo(struct addrinfo *res);
  142. int ff_getnameinfo(const struct sockaddr *sa, int salen,
  143. char *host, int hostlen,
  144. char *serv, int servlen, int flags);
  145. const char *ff_gai_strerror(int ecode);
  146. #define getaddrinfo ff_getaddrinfo
  147. #define freeaddrinfo ff_freeaddrinfo
  148. #define getnameinfo ff_getnameinfo
  149. #define gai_strerror ff_gai_strerror
  150. #endif
  151. #ifndef INET6_ADDRSTRLEN
  152. #define INET6_ADDRSTRLEN INET_ADDRSTRLEN
  153. #endif
  154. #ifndef IN_MULTICAST
  155. #define IN_MULTICAST(a) ((((uint32_t)(a)) & 0xf0000000) == 0xe0000000)
  156. #endif
  157. #ifndef IN6_IS_ADDR_MULTICAST
  158. #define IN6_IS_ADDR_MULTICAST(a) (((uint8_t *) (a))[0] == 0xff)
  159. #endif
  160. static inline int ff_is_multicast_address(struct sockaddr *addr)
  161. {
  162. if (addr->sa_family == AF_INET) {
  163. return IN_MULTICAST(ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr));
  164. }
  165. #if HAVE_STRUCT_SOCKADDR_IN6
  166. if (addr->sa_family == AF_INET6) {
  167. return IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)addr)->sin6_addr);
  168. }
  169. #endif
  170. return 0;
  171. }
  172. #endif /* AVFORMAT_NETWORK_H */