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.

191 lines
4.1KB

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