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.

256 lines
6.2KB

  1. /*
  2. * Copyright (c) 2007 The Libav Project
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav 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. * Libav 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 Libav; 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 "url.h"
  28. #if HAVE_UNISTD_H
  29. #include <unistd.h>
  30. #endif
  31. #if HAVE_WINSOCK2_H
  32. #include <winsock2.h>
  33. #include <ws2tcpip.h>
  34. #ifndef EPROTONOSUPPORT
  35. #define EPROTONOSUPPORT WSAEPROTONOSUPPORT
  36. #endif
  37. #ifndef ETIMEDOUT
  38. #define ETIMEDOUT WSAETIMEDOUT
  39. #endif
  40. #ifndef ECONNREFUSED
  41. #define ECONNREFUSED WSAECONNREFUSED
  42. #endif
  43. #ifndef EINPROGRESS
  44. #define EINPROGRESS WSAEINPROGRESS
  45. #endif
  46. #define getsockopt(a, b, c, d, e) getsockopt(a, b, c, (char*) d, e)
  47. #define setsockopt(a, b, c, d, e) setsockopt(a, b, c, (const char*) d, e)
  48. int ff_neterrno(void);
  49. #else
  50. #include <sys/types.h>
  51. #include <sys/socket.h>
  52. #include <netinet/in.h>
  53. #include <netdb.h>
  54. #define ff_neterrno() AVERROR(errno)
  55. #endif /* HAVE_WINSOCK2_H */
  56. #if HAVE_ARPA_INET_H
  57. #include <arpa/inet.h>
  58. #endif
  59. #if HAVE_POLL_H
  60. #include <poll.h>
  61. #endif
  62. int ff_socket_nonblock(int socket, int enable);
  63. extern int ff_network_inited_globally;
  64. int ff_network_init(void);
  65. void ff_network_close(void);
  66. void ff_tls_init(void);
  67. void ff_tls_deinit(void);
  68. int ff_network_wait_fd(int fd, int write);
  69. int ff_inet_aton (const char * str, struct in_addr * add);
  70. #if !HAVE_STRUCT_SOCKADDR_STORAGE
  71. struct sockaddr_storage {
  72. #if HAVE_STRUCT_SOCKADDR_SA_LEN
  73. uint8_t ss_len;
  74. uint8_t ss_family;
  75. #else
  76. uint16_t ss_family;
  77. #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
  78. char ss_pad1[6];
  79. int64_t ss_align;
  80. char ss_pad2[112];
  81. };
  82. #endif /* !HAVE_STRUCT_SOCKADDR_STORAGE */
  83. #if !HAVE_STRUCT_ADDRINFO
  84. struct addrinfo {
  85. int ai_flags;
  86. int ai_family;
  87. int ai_socktype;
  88. int ai_protocol;
  89. int ai_addrlen;
  90. struct sockaddr *ai_addr;
  91. char *ai_canonname;
  92. struct addrinfo *ai_next;
  93. };
  94. #endif /* !HAVE_STRUCT_ADDRINFO */
  95. /* getaddrinfo constants */
  96. #ifndef EAI_AGAIN
  97. #define EAI_AGAIN 2
  98. #endif
  99. #ifndef EAI_BADFLAGS
  100. #define EAI_BADFLAGS 3
  101. #endif
  102. #ifndef EAI_FAIL
  103. #define EAI_FAIL 4
  104. #endif
  105. #ifndef EAI_FAMILY
  106. #define EAI_FAMILY 5
  107. #endif
  108. #ifndef EAI_MEMORY
  109. #define EAI_MEMORY 6
  110. #endif
  111. #ifndef EAI_NODATA
  112. #define EAI_NODATA 7
  113. #endif
  114. #ifndef EAI_NONAME
  115. #define EAI_NONAME 8
  116. #endif
  117. #ifndef EAI_SERVICE
  118. #define EAI_SERVICE 9
  119. #endif
  120. #ifndef EAI_SOCKTYPE
  121. #define EAI_SOCKTYPE 10
  122. #endif
  123. #ifndef AI_PASSIVE
  124. #define AI_PASSIVE 1
  125. #endif
  126. #ifndef AI_CANONNAME
  127. #define AI_CANONNAME 2
  128. #endif
  129. #ifndef AI_NUMERICHOST
  130. #define AI_NUMERICHOST 4
  131. #endif
  132. #ifndef NI_NOFQDN
  133. #define NI_NOFQDN 1
  134. #endif
  135. #ifndef NI_NUMERICHOST
  136. #define NI_NUMERICHOST 2
  137. #endif
  138. #ifndef NI_NAMERQD
  139. #define NI_NAMERQD 4
  140. #endif
  141. #ifndef NI_NUMERICSERV
  142. #define NI_NUMERICSERV 8
  143. #endif
  144. #ifndef NI_DGRAM
  145. #define NI_DGRAM 16
  146. #endif
  147. #if !HAVE_GETADDRINFO
  148. int ff_getaddrinfo(const char *node, const char *service,
  149. const struct addrinfo *hints, struct addrinfo **res);
  150. void ff_freeaddrinfo(struct addrinfo *res);
  151. int ff_getnameinfo(const struct sockaddr *sa, int salen,
  152. char *host, int hostlen,
  153. char *serv, int servlen, int flags);
  154. #define getaddrinfo ff_getaddrinfo
  155. #define freeaddrinfo ff_freeaddrinfo
  156. #define getnameinfo ff_getnameinfo
  157. #endif /* !HAVE_GETADDRINFO */
  158. #if !HAVE_GETADDRINFO || HAVE_WINSOCK2_H
  159. const char *ff_gai_strerror(int ecode);
  160. #undef gai_strerror
  161. #define gai_strerror ff_gai_strerror
  162. #endif /* !HAVE_GETADDRINFO || HAVE_WINSOCK2_H */
  163. #ifndef INADDR_LOOPBACK
  164. #define INADDR_LOOPBACK 0x7f000001
  165. #endif
  166. #ifndef INET_ADDRSTRLEN
  167. #define INET_ADDRSTRLEN 16
  168. #endif
  169. #ifndef INET6_ADDRSTRLEN
  170. #define INET6_ADDRSTRLEN INET_ADDRSTRLEN
  171. #endif
  172. #ifndef IN_MULTICAST
  173. #define IN_MULTICAST(a) ((((uint32_t)(a)) & 0xf0000000) == 0xe0000000)
  174. #endif
  175. #ifndef IN6_IS_ADDR_MULTICAST
  176. #define IN6_IS_ADDR_MULTICAST(a) (((uint8_t *) (a))[0] == 0xff)
  177. #endif
  178. int ff_is_multicast_address(struct sockaddr *addr);
  179. #define POLLING_TIME 100 /// Time in milliseconds between interrupt check
  180. /**
  181. * Bind to a file descriptor and poll for a connection.
  182. *
  183. * @param fd First argument of bind().
  184. * @param addr Second argument of bind().
  185. * @param addrlen Third argument of bind().
  186. * @param timeout Polling timeout in milliseconds.
  187. * @param h URLContext providing interrupt check
  188. * callback and logging context.
  189. * @return A non-blocking file descriptor on success
  190. * or an AVERROR on failure.
  191. */
  192. int ff_listen_bind(int fd, const struct sockaddr *addr,
  193. socklen_t addrlen, int timeout,
  194. URLContext *h);
  195. /**
  196. * Connect to a file descriptor and poll for result.
  197. *
  198. * @param fd First argument of connect(),
  199. * will be set as non-blocking.
  200. * @param addr Second argument of connect().
  201. * @param addrlen Third argument of connect().
  202. * @param timeout Polling timeout in milliseconds.
  203. * @param h URLContext providing interrupt check
  204. * callback and logging context.
  205. * @param will_try_next Whether the caller will try to connect to another
  206. * address for the same host name, affecting the form of
  207. * logged errors.
  208. * @return 0 on success, AVERROR on failure.
  209. */
  210. int ff_listen_connect(int fd, const struct sockaddr *addr,
  211. socklen_t addrlen, int timeout,
  212. URLContext *h, int will_try_next);
  213. int ff_http_match_no_proxy(const char *no_proxy, const char *hostname);
  214. int ff_socket(int domain, int type, int protocol);
  215. #endif /* AVFORMAT_NETWORK_H */