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.

281 lines
7.0KB

  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. 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. 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. typedef union sockaddr_union {
  96. struct sockaddr_storage storage;
  97. struct sockaddr_in in;
  98. #if HAVE_STRUCT_SOCKADDR_IN6
  99. struct sockaddr_in6 in6;
  100. #endif
  101. } sockaddr_union;
  102. #ifndef MSG_NOSIGNAL
  103. #define MSG_NOSIGNAL 0
  104. #endif
  105. #if !HAVE_STRUCT_ADDRINFO
  106. struct addrinfo {
  107. int ai_flags;
  108. int ai_family;
  109. int ai_socktype;
  110. int ai_protocol;
  111. int ai_addrlen;
  112. struct sockaddr *ai_addr;
  113. char *ai_canonname;
  114. struct addrinfo *ai_next;
  115. };
  116. #endif /* !HAVE_STRUCT_ADDRINFO */
  117. /* getaddrinfo constants */
  118. #ifndef EAI_AGAIN
  119. #define EAI_AGAIN 2
  120. #endif
  121. #ifndef EAI_BADFLAGS
  122. #define EAI_BADFLAGS 3
  123. #endif
  124. #ifndef EAI_FAIL
  125. #define EAI_FAIL 4
  126. #endif
  127. #ifndef EAI_FAMILY
  128. #define EAI_FAMILY 5
  129. #endif
  130. #ifndef EAI_MEMORY
  131. #define EAI_MEMORY 6
  132. #endif
  133. #ifndef EAI_NODATA
  134. #define EAI_NODATA 7
  135. #endif
  136. #ifndef EAI_NONAME
  137. #define EAI_NONAME 8
  138. #endif
  139. #ifndef EAI_SERVICE
  140. #define EAI_SERVICE 9
  141. #endif
  142. #ifndef EAI_SOCKTYPE
  143. #define EAI_SOCKTYPE 10
  144. #endif
  145. #ifndef AI_PASSIVE
  146. #define AI_PASSIVE 1
  147. #endif
  148. #ifndef AI_CANONNAME
  149. #define AI_CANONNAME 2
  150. #endif
  151. #ifndef AI_NUMERICHOST
  152. #define AI_NUMERICHOST 4
  153. #endif
  154. #ifndef NI_NOFQDN
  155. #define NI_NOFQDN 1
  156. #endif
  157. #ifndef NI_NUMERICHOST
  158. #define NI_NUMERICHOST 2
  159. #endif
  160. #ifndef NI_NAMERQD
  161. #define NI_NAMERQD 4
  162. #endif
  163. #ifndef NI_NUMERICSERV
  164. #define NI_NUMERICSERV 8
  165. #endif
  166. #ifndef NI_DGRAM
  167. #define NI_DGRAM 16
  168. #endif
  169. #if !HAVE_GETADDRINFO
  170. int ff_getaddrinfo(const char *node, const char *service,
  171. const struct addrinfo *hints, struct addrinfo **res);
  172. void ff_freeaddrinfo(struct addrinfo *res);
  173. int ff_getnameinfo(const struct sockaddr *sa, int salen,
  174. char *host, int hostlen,
  175. char *serv, int servlen, int flags);
  176. #define getaddrinfo ff_getaddrinfo
  177. #define freeaddrinfo ff_freeaddrinfo
  178. #define getnameinfo ff_getnameinfo
  179. #endif /* !HAVE_GETADDRINFO */
  180. #if !HAVE_GETADDRINFO || HAVE_WINSOCK2_H
  181. const char *ff_gai_strerror(int ecode);
  182. #undef gai_strerror
  183. #define gai_strerror ff_gai_strerror
  184. #endif /* !HAVE_GETADDRINFO || HAVE_WINSOCK2_H */
  185. #ifndef INADDR_LOOPBACK
  186. #define INADDR_LOOPBACK 0x7f000001
  187. #endif
  188. #ifndef INET_ADDRSTRLEN
  189. #define INET_ADDRSTRLEN 16
  190. #endif
  191. #ifndef INET6_ADDRSTRLEN
  192. #define INET6_ADDRSTRLEN INET_ADDRSTRLEN
  193. #endif
  194. #ifndef IN_MULTICAST
  195. #define IN_MULTICAST(a) ((((uint32_t)(a)) & 0xf0000000) == 0xe0000000)
  196. #endif
  197. #ifndef IN6_IS_ADDR_MULTICAST
  198. #define IN6_IS_ADDR_MULTICAST(a) (((uint8_t *) (a))[0] == 0xff)
  199. #endif
  200. int ff_is_multicast_address(struct sockaddr *addr);
  201. #define POLLING_TIME 100 /// Time in milliseconds between interrupt check
  202. /**
  203. * Bind to a file descriptor and poll for a connection.
  204. *
  205. * @param fd First argument of bind().
  206. * @param addr Second argument of bind().
  207. * @param addrlen Third argument of bind().
  208. * @param timeout Polling timeout in milliseconds.
  209. * @param h URLContext providing interrupt check
  210. * callback and logging context.
  211. * @return A non-blocking file descriptor on success
  212. * or an AVERROR on failure.
  213. */
  214. int ff_listen_bind(int fd, const struct sockaddr *addr,
  215. socklen_t addrlen, int timeout,
  216. URLContext *h);
  217. /**
  218. * Connect to a file descriptor and poll for result.
  219. *
  220. * @param fd First argument of connect(),
  221. * will be set as non-blocking.
  222. * @param addr Second argument of connect().
  223. * @param addrlen Third argument of connect().
  224. * @param timeout Polling timeout in milliseconds.
  225. * @param h URLContext providing interrupt check
  226. * callback and logging context.
  227. * @param will_try_next Whether the caller will try to connect to another
  228. * address for the same host name, affecting the form of
  229. * logged errors.
  230. * @return 0 on success, AVERROR on failure.
  231. */
  232. int ff_listen_connect(int fd, const struct sockaddr *addr,
  233. socklen_t addrlen, int timeout,
  234. URLContext *h, int will_try_next);
  235. int ff_http_match_no_proxy(const char *no_proxy, const char *hostname);
  236. int ff_socket(int domain, int type, int protocol);
  237. #endif /* AVFORMAT_NETWORK_H */