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.

269 lines
6.8KB

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