network.h 4.5 KB

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