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.

260 lines
6.2KB

  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 <errno.h>
  23. #include <stdint.h>
  24. #include "config.h"
  25. #include "libavutil/error.h"
  26. #include "os_support.h"
  27. #include "url.h"
  28. #if HAVE_UNISTD_H
  29. #include <unistd.h>
  30. #endif
  31. #if HAVE_WINSOCK2_H
  32. #include <winsock2.h>
  33. #include <ws2tcpip.h>
  34. #ifndef EPROTONOSUPPORT
  35. #define EPROTONOSUPPORT WSAEPROTONOSUPPORT
  36. #endif
  37. #ifndef ETIMEDOUT
  38. #define ETIMEDOUT WSAETIMEDOUT
  39. #endif
  40. #ifndef ECONNREFUSED
  41. #define ECONNREFUSED WSAECONNREFUSED
  42. #endif
  43. #ifndef EINPROGRESS
  44. #define EINPROGRESS WSAEINPROGRESS
  45. #endif
  46. #define getsockopt(a, b, c, d, e) getsockopt(a, b, c, (char*) d, e)
  47. #define setsockopt(a, b, c, d, e) setsockopt(a, b, c, (const char*) d, e)
  48. int ff_neterrno(void);
  49. #else
  50. #include <sys/types.h>
  51. #include <sys/socket.h>
  52. #include <netinet/in.h>
  53. #include <netdb.h>
  54. #define ff_neterrno() AVERROR(errno)
  55. #endif /* HAVE_WINSOCK2_H */
  56. #if HAVE_ARPA_INET_H
  57. #include <arpa/inet.h>
  58. #endif
  59. #if HAVE_POLL_H
  60. #include <poll.h>
  61. #endif
  62. int ff_socket_nonblock(int socket, int enable);
  63. extern int ff_network_inited_globally;
  64. int ff_network_init(void);
  65. void ff_network_close(void);
  66. void ff_tls_init(void);
  67. void ff_tls_deinit(void);
  68. int ff_network_wait_fd(int fd, int write);
  69. int ff_inet_aton (const char * str, struct in_addr * add);
  70. #if !HAVE_STRUCT_SOCKADDR_STORAGE
  71. struct sockaddr_storage {
  72. #if HAVE_STRUCT_SOCKADDR_SA_LEN
  73. uint8_t ss_len;
  74. uint8_t ss_family;
  75. #else
  76. uint16_t ss_family;
  77. #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
  78. char ss_pad1[6];
  79. int64_t ss_align;
  80. char ss_pad2[112];
  81. };
  82. #endif /* !HAVE_STRUCT_SOCKADDR_STORAGE */
  83. #ifndef MSG_NOSIGNAL
  84. #define MSG_NOSIGNAL 0
  85. #endif
  86. #if !HAVE_STRUCT_ADDRINFO
  87. struct addrinfo {
  88. int ai_flags;
  89. int ai_family;
  90. int ai_socktype;
  91. int ai_protocol;
  92. int ai_addrlen;
  93. struct sockaddr *ai_addr;
  94. char *ai_canonname;
  95. struct addrinfo *ai_next;
  96. };
  97. #endif /* !HAVE_STRUCT_ADDRINFO */
  98. /* getaddrinfo constants */
  99. #ifndef EAI_AGAIN
  100. #define EAI_AGAIN 2
  101. #endif
  102. #ifndef EAI_BADFLAGS
  103. #define EAI_BADFLAGS 3
  104. #endif
  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_MEMORY
  112. #define EAI_MEMORY 6
  113. #endif
  114. #ifndef EAI_NODATA
  115. #define EAI_NODATA 7
  116. #endif
  117. #ifndef EAI_NONAME
  118. #define EAI_NONAME 8
  119. #endif
  120. #ifndef EAI_SERVICE
  121. #define EAI_SERVICE 9
  122. #endif
  123. #ifndef EAI_SOCKTYPE
  124. #define EAI_SOCKTYPE 10
  125. #endif
  126. #ifndef AI_PASSIVE
  127. #define AI_PASSIVE 1
  128. #endif
  129. #ifndef AI_CANONNAME
  130. #define AI_CANONNAME 2
  131. #endif
  132. #ifndef AI_NUMERICHOST
  133. #define AI_NUMERICHOST 4
  134. #endif
  135. #ifndef NI_NOFQDN
  136. #define NI_NOFQDN 1
  137. #endif
  138. #ifndef NI_NUMERICHOST
  139. #define NI_NUMERICHOST 2
  140. #endif
  141. #ifndef NI_NAMERQD
  142. #define NI_NAMERQD 4
  143. #endif
  144. #ifndef NI_NUMERICSERV
  145. #define NI_NUMERICSERV 8
  146. #endif
  147. #ifndef NI_DGRAM
  148. #define NI_DGRAM 16
  149. #endif
  150. #if !HAVE_GETADDRINFO
  151. int ff_getaddrinfo(const char *node, const char *service,
  152. const struct addrinfo *hints, struct addrinfo **res);
  153. void ff_freeaddrinfo(struct addrinfo *res);
  154. int ff_getnameinfo(const struct sockaddr *sa, int salen,
  155. char *host, int hostlen,
  156. char *serv, int servlen, int flags);
  157. #define getaddrinfo ff_getaddrinfo
  158. #define freeaddrinfo ff_freeaddrinfo
  159. #define getnameinfo ff_getnameinfo
  160. #endif /* !HAVE_GETADDRINFO */
  161. #if !HAVE_GETADDRINFO || HAVE_WINSOCK2_H
  162. const char *ff_gai_strerror(int ecode);
  163. #undef gai_strerror
  164. #define gai_strerror ff_gai_strerror
  165. #endif /* !HAVE_GETADDRINFO || HAVE_WINSOCK2_H */
  166. #ifndef INADDR_LOOPBACK
  167. #define INADDR_LOOPBACK 0x7f000001
  168. #endif
  169. #ifndef INET_ADDRSTRLEN
  170. #define INET_ADDRSTRLEN 16
  171. #endif
  172. #ifndef INET6_ADDRSTRLEN
  173. #define INET6_ADDRSTRLEN INET_ADDRSTRLEN
  174. #endif
  175. #ifndef IN_MULTICAST
  176. #define IN_MULTICAST(a) ((((uint32_t)(a)) & 0xf0000000) == 0xe0000000)
  177. #endif
  178. #ifndef IN6_IS_ADDR_MULTICAST
  179. #define IN6_IS_ADDR_MULTICAST(a) (((uint8_t *) (a))[0] == 0xff)
  180. #endif
  181. int ff_is_multicast_address(struct sockaddr *addr);
  182. #define POLLING_TIME 100 /// Time in milliseconds between interrupt check
  183. /**
  184. * Bind to a file descriptor and poll for a connection.
  185. *
  186. * @param fd First argument of bind().
  187. * @param addr Second argument of bind().
  188. * @param addrlen Third argument of bind().
  189. * @param timeout Polling timeout in milliseconds.
  190. * @param h URLContext providing interrupt check
  191. * callback and logging context.
  192. * @return A non-blocking file descriptor on success
  193. * or an AVERROR on failure.
  194. */
  195. int ff_listen_bind(int fd, const struct sockaddr *addr,
  196. socklen_t addrlen, int timeout,
  197. URLContext *h);
  198. /**
  199. * Connect to a file descriptor and poll for result.
  200. *
  201. * @param fd First argument of connect(),
  202. * will be set as non-blocking.
  203. * @param addr Second argument of connect().
  204. * @param addrlen Third argument of connect().
  205. * @param timeout Polling timeout in milliseconds.
  206. * @param h URLContext providing interrupt check
  207. * callback and logging context.
  208. * @param will_try_next Whether the caller will try to connect to another
  209. * address for the same host name, affecting the form of
  210. * logged errors.
  211. * @return 0 on success, AVERROR on failure.
  212. */
  213. int ff_listen_connect(int fd, const struct sockaddr *addr,
  214. socklen_t addrlen, int timeout,
  215. URLContext *h, int will_try_next);
  216. int ff_http_match_no_proxy(const char *no_proxy, const char *hostname);
  217. int ff_socket(int domain, int type, int protocol);
  218. #endif /* AVFORMAT_NETWORK_H */