network.h 8.1 KB

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