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.

311 lines
8.2KB

  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 <netinet/tcp.h>
  55. #include <netdb.h>
  56. #define ff_neterrno() AVERROR(errno)
  57. #endif /* HAVE_WINSOCK2_H */
  58. #if HAVE_ARPA_INET_H
  59. #include <arpa/inet.h>
  60. #endif
  61. #if HAVE_POLL_H
  62. #include <poll.h>
  63. #endif
  64. int ff_socket_nonblock(int socket, int enable);
  65. extern int ff_network_inited_globally;
  66. int ff_network_init(void);
  67. void ff_network_close(void);
  68. int ff_tls_init(void);
  69. void ff_tls_deinit(void);
  70. int ff_network_wait_fd(int fd, int write);
  71. /**
  72. * This works similarly to ff_network_wait_fd, but waits up to 'timeout' microseconds
  73. * Uses ff_network_wait_fd in a loop
  74. *
  75. * @fd Socket descriptor
  76. * @write Set 1 to wait for socket able to be read, 0 to be written
  77. * @timeout Timeout interval, in microseconds. Actual precision is 100000 mcs, due to ff_network_wait_fd usage
  78. * @param int_cb Interrupt callback, is checked before each ff_network_wait_fd call
  79. * @return 0 if data can be read/written, AVERROR(ETIMEDOUT) if timeout expired, or negative error code
  80. */
  81. int ff_network_wait_fd_timeout(int fd, int write, int64_t timeout, AVIOInterruptCB *int_cb);
  82. /**
  83. * Waits for up to 'timeout' microseconds. If the usert's int_cb is set and
  84. * triggered, return before that.
  85. * @timeout Timeout in microseconds. Maybe have lower actual precision.
  86. * @param int_cb Interrupt callback, is checked regularly.
  87. * @return AVERROR(ETIMEDOUT) if timeout expirted, AVERROR_EXIT if interrupted by int_cb
  88. */
  89. int ff_network_sleep_interruptible(int64_t timeout, AVIOInterruptCB *int_cb);
  90. int ff_inet_aton (const char * str, struct in_addr * add);
  91. #if !HAVE_STRUCT_SOCKADDR_STORAGE
  92. struct sockaddr_storage {
  93. #if HAVE_STRUCT_SOCKADDR_SA_LEN
  94. uint8_t ss_len;
  95. uint8_t ss_family;
  96. #else
  97. uint16_t ss_family;
  98. #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
  99. char ss_pad1[6];
  100. int64_t ss_align;
  101. char ss_pad2[112];
  102. };
  103. #endif /* !HAVE_STRUCT_SOCKADDR_STORAGE */
  104. typedef union sockaddr_union {
  105. struct sockaddr_storage storage;
  106. struct sockaddr_in in;
  107. #if HAVE_STRUCT_SOCKADDR_IN6
  108. struct sockaddr_in6 in6;
  109. #endif
  110. } sockaddr_union;
  111. #ifndef MSG_NOSIGNAL
  112. #define MSG_NOSIGNAL 0
  113. #endif
  114. #if !HAVE_STRUCT_ADDRINFO
  115. struct addrinfo {
  116. int ai_flags;
  117. int ai_family;
  118. int ai_socktype;
  119. int ai_protocol;
  120. int ai_addrlen;
  121. struct sockaddr *ai_addr;
  122. char *ai_canonname;
  123. struct addrinfo *ai_next;
  124. };
  125. #endif /* !HAVE_STRUCT_ADDRINFO */
  126. /* getaddrinfo constants */
  127. #ifndef EAI_AGAIN
  128. #define EAI_AGAIN 2
  129. #endif
  130. #ifndef EAI_BADFLAGS
  131. #define EAI_BADFLAGS 3
  132. #endif
  133. #ifndef EAI_FAIL
  134. #define EAI_FAIL 4
  135. #endif
  136. #ifndef EAI_FAMILY
  137. #define EAI_FAMILY 5
  138. #endif
  139. #ifndef EAI_MEMORY
  140. #define EAI_MEMORY 6
  141. #endif
  142. #ifndef EAI_NODATA
  143. #define EAI_NODATA 7
  144. #endif
  145. #ifndef EAI_NONAME
  146. #define EAI_NONAME 8
  147. #endif
  148. #ifndef EAI_SERVICE
  149. #define EAI_SERVICE 9
  150. #endif
  151. #ifndef EAI_SOCKTYPE
  152. #define EAI_SOCKTYPE 10
  153. #endif
  154. #ifndef AI_PASSIVE
  155. #define AI_PASSIVE 1
  156. #endif
  157. #ifndef AI_CANONNAME
  158. #define AI_CANONNAME 2
  159. #endif
  160. #ifndef AI_NUMERICHOST
  161. #define AI_NUMERICHOST 4
  162. #endif
  163. #ifndef NI_NOFQDN
  164. #define NI_NOFQDN 1
  165. #endif
  166. #ifndef NI_NUMERICHOST
  167. #define NI_NUMERICHOST 2
  168. #endif
  169. #ifndef NI_NAMERQD
  170. #define NI_NAMERQD 4
  171. #endif
  172. #ifndef NI_NUMERICSERV
  173. #define NI_NUMERICSERV 8
  174. #endif
  175. #ifndef NI_DGRAM
  176. #define NI_DGRAM 16
  177. #endif
  178. #if !HAVE_GETADDRINFO
  179. int ff_getaddrinfo(const char *node, const char *service,
  180. const struct addrinfo *hints, struct addrinfo **res);
  181. void ff_freeaddrinfo(struct addrinfo *res);
  182. int ff_getnameinfo(const struct sockaddr *sa, int salen,
  183. char *host, int hostlen,
  184. char *serv, int servlen, int flags);
  185. #define getaddrinfo ff_getaddrinfo
  186. #define freeaddrinfo ff_freeaddrinfo
  187. #define getnameinfo ff_getnameinfo
  188. #endif /* !HAVE_GETADDRINFO */
  189. #if !HAVE_GETADDRINFO || HAVE_WINSOCK2_H
  190. const char *ff_gai_strerror(int ecode);
  191. #undef gai_strerror
  192. #define gai_strerror ff_gai_strerror
  193. #endif /* !HAVE_GETADDRINFO || HAVE_WINSOCK2_H */
  194. #ifndef INADDR_LOOPBACK
  195. #define INADDR_LOOPBACK 0x7f000001
  196. #endif
  197. #ifndef INET_ADDRSTRLEN
  198. #define INET_ADDRSTRLEN 16
  199. #endif
  200. #ifndef INET6_ADDRSTRLEN
  201. #define INET6_ADDRSTRLEN INET_ADDRSTRLEN
  202. #endif
  203. #ifndef IN_MULTICAST
  204. #define IN_MULTICAST(a) ((((uint32_t)(a)) & 0xf0000000) == 0xe0000000)
  205. #endif
  206. #ifndef IN6_IS_ADDR_MULTICAST
  207. #define IN6_IS_ADDR_MULTICAST(a) (((uint8_t *) (a))[0] == 0xff)
  208. #endif
  209. int ff_is_multicast_address(struct sockaddr *addr);
  210. #define POLLING_TIME 100 /// Time in milliseconds between interrupt check
  211. /**
  212. * Bind to a file descriptor and poll for a connection.
  213. *
  214. * @param fd First argument of bind().
  215. * @param addr Second argument of bind().
  216. * @param addrlen Third argument of bind().
  217. * @param timeout Polling timeout in milliseconds.
  218. * @param h URLContext providing interrupt check
  219. * callback and logging context.
  220. * @return A non-blocking file descriptor on success
  221. * or an AVERROR on failure.
  222. */
  223. int ff_listen_bind(int fd, const struct sockaddr *addr,
  224. socklen_t addrlen, int timeout,
  225. URLContext *h);
  226. /**
  227. * Bind to a file descriptor to an address without accepting connections.
  228. * @param fd First argument of bind().
  229. * @param addr Second argument of bind().
  230. * @param addrlen Third argument of bind().
  231. * @return 0 on success or an AVERROR on failure.
  232. */
  233. int ff_listen(int fd, const struct sockaddr *addr, socklen_t addrlen);
  234. /**
  235. * Poll for a single connection on the passed file descriptor.
  236. * @param fd The listening socket file descriptor.
  237. * @param timeout Polling timeout in milliseconds.
  238. * @param h URLContext providing interrupt check
  239. * callback and logging context.
  240. * @return A non-blocking file descriptor on success
  241. * or an AVERROR on failure.
  242. */
  243. int ff_accept(int fd, int timeout, URLContext *h);
  244. /**
  245. * Connect to a file descriptor and poll for result.
  246. *
  247. * @param fd First argument of connect(),
  248. * will be set as non-blocking.
  249. * @param addr Second argument of connect().
  250. * @param addrlen Third argument of connect().
  251. * @param timeout Polling timeout in milliseconds.
  252. * @param h URLContext providing interrupt check
  253. * callback and logging context.
  254. * @param will_try_next Whether the caller will try to connect to another
  255. * address for the same host name, affecting the form of
  256. * logged errors.
  257. * @return 0 on success, AVERROR on failure.
  258. */
  259. int ff_listen_connect(int fd, const struct sockaddr *addr,
  260. socklen_t addrlen, int timeout,
  261. URLContext *h, int will_try_next);
  262. int ff_http_match_no_proxy(const char *no_proxy, const char *hostname);
  263. int ff_socket(int domain, int type, int protocol);
  264. #endif /* AVFORMAT_NETWORK_H */