network.h 9.8 KB

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