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.

230 lines
6.1KB

  1. /*
  2. * TCP protocol
  3. * Copyright (c) 2002 Fabrice Bellard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avformat.h"
  22. #include <unistd.h>
  23. #include "internal.h"
  24. #include "network.h"
  25. #include "os_support.h"
  26. #if HAVE_SYS_SELECT_H
  27. #include <sys/select.h>
  28. #endif
  29. #include <sys/time.h>
  30. typedef struct TCPContext {
  31. int fd;
  32. } TCPContext;
  33. /* return non zero if error */
  34. static int tcp_open(URLContext *h, const char *uri, int flags)
  35. {
  36. struct addrinfo hints, *ai, *cur_ai;
  37. int port, fd = -1;
  38. TCPContext *s = NULL;
  39. fd_set wfds, efds;
  40. int fd_max, ret;
  41. struct timeval tv;
  42. socklen_t optlen;
  43. char hostname[1024],proto[1024],path[1024];
  44. char portstr[10];
  45. av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
  46. &port, path, sizeof(path), uri);
  47. if (strcmp(proto,"tcp") || port <= 0 || port >= 65536)
  48. return AVERROR(EINVAL);
  49. memset(&hints, 0, sizeof(hints));
  50. hints.ai_family = AF_UNSPEC;
  51. hints.ai_socktype = SOCK_STREAM;
  52. snprintf(portstr, sizeof(portstr), "%d", port);
  53. ret = getaddrinfo(hostname, portstr, &hints, &ai);
  54. if (ret) {
  55. av_log(NULL, AV_LOG_ERROR,
  56. "Failed to resolve hostname %s: %s\n",
  57. hostname, gai_strerror(ret));
  58. return AVERROR(EIO);
  59. }
  60. cur_ai = ai;
  61. restart:
  62. fd = socket(cur_ai->ai_family, cur_ai->ai_socktype, cur_ai->ai_protocol);
  63. if (fd < 0)
  64. goto fail;
  65. ff_socket_nonblock(fd, 1);
  66. redo:
  67. ret = connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
  68. if (ret < 0) {
  69. if (ff_neterrno() == FF_NETERROR(EINTR)) {
  70. if (url_interrupt_cb())
  71. goto fail1;
  72. goto redo;
  73. }
  74. if (ff_neterrno() != FF_NETERROR(EINPROGRESS) &&
  75. ff_neterrno() != FF_NETERROR(EAGAIN))
  76. goto fail;
  77. /* wait until we are connected or until abort */
  78. for(;;) {
  79. if (url_interrupt_cb()) {
  80. ret = AVERROR(EINTR);
  81. goto fail1;
  82. }
  83. fd_max = fd;
  84. FD_ZERO(&wfds);
  85. FD_ZERO(&efds);
  86. FD_SET(fd, &wfds);
  87. FD_SET(fd, &efds);
  88. tv.tv_sec = 0;
  89. tv.tv_usec = 100 * 1000;
  90. ret = select(fd_max + 1, NULL, &wfds, &efds, &tv);
  91. if (ret > 0 && (FD_ISSET(fd, &wfds) || FD_ISSET(fd, &efds)))
  92. break;
  93. }
  94. /* test error */
  95. optlen = sizeof(ret);
  96. getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen);
  97. if (ret != 0) {
  98. av_log(NULL, AV_LOG_ERROR,
  99. "TCP connection to %s:%d failed: %s\n",
  100. hostname, port, strerror(ret));
  101. goto fail;
  102. }
  103. }
  104. s = av_malloc(sizeof(TCPContext));
  105. if (!s) {
  106. freeaddrinfo(ai);
  107. return AVERROR(ENOMEM);
  108. }
  109. h->priv_data = s;
  110. h->is_streamed = 1;
  111. s->fd = fd;
  112. freeaddrinfo(ai);
  113. return 0;
  114. fail:
  115. if (cur_ai->ai_next) {
  116. /* Retry with the next sockaddr */
  117. cur_ai = cur_ai->ai_next;
  118. if (fd >= 0)
  119. closesocket(fd);
  120. goto restart;
  121. }
  122. ret = AVERROR(EIO);
  123. fail1:
  124. if (fd >= 0)
  125. closesocket(fd);
  126. freeaddrinfo(ai);
  127. return ret;
  128. }
  129. static int tcp_read(URLContext *h, uint8_t *buf, int size)
  130. {
  131. TCPContext *s = h->priv_data;
  132. int len, fd_max, ret;
  133. fd_set rfds;
  134. struct timeval tv;
  135. for (;;) {
  136. if (url_interrupt_cb())
  137. return AVERROR(EINTR);
  138. fd_max = s->fd;
  139. FD_ZERO(&rfds);
  140. FD_SET(s->fd, &rfds);
  141. tv.tv_sec = 0;
  142. tv.tv_usec = 100 * 1000;
  143. ret = select(fd_max + 1, &rfds, NULL, NULL, &tv);
  144. if (ret > 0 && FD_ISSET(s->fd, &rfds)) {
  145. len = recv(s->fd, buf, size, 0);
  146. if (len < 0) {
  147. if (ff_neterrno() != FF_NETERROR(EINTR) &&
  148. ff_neterrno() != FF_NETERROR(EAGAIN))
  149. return ff_neterrno();
  150. } else return len;
  151. } else if (ret < 0) {
  152. if (ff_neterrno() == FF_NETERROR(EINTR))
  153. continue;
  154. return -1;
  155. }
  156. }
  157. }
  158. static int tcp_write(URLContext *h, const uint8_t *buf, int size)
  159. {
  160. TCPContext *s = h->priv_data;
  161. int ret, size1, fd_max, len;
  162. fd_set wfds;
  163. struct timeval tv;
  164. size1 = size;
  165. while (size > 0) {
  166. if (url_interrupt_cb())
  167. return AVERROR(EINTR);
  168. fd_max = s->fd;
  169. FD_ZERO(&wfds);
  170. FD_SET(s->fd, &wfds);
  171. tv.tv_sec = 0;
  172. tv.tv_usec = 100 * 1000;
  173. ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);
  174. if (ret > 0 && FD_ISSET(s->fd, &wfds)) {
  175. len = send(s->fd, buf, size, 0);
  176. if (len < 0) {
  177. if (ff_neterrno() != FF_NETERROR(EINTR) &&
  178. ff_neterrno() != FF_NETERROR(EAGAIN))
  179. return ff_neterrno();
  180. continue;
  181. }
  182. size -= len;
  183. buf += len;
  184. } else if (ret < 0) {
  185. if (ff_neterrno() == FF_NETERROR(EINTR))
  186. continue;
  187. return -1;
  188. }
  189. }
  190. return size1 - size;
  191. }
  192. static int tcp_close(URLContext *h)
  193. {
  194. TCPContext *s = h->priv_data;
  195. closesocket(s->fd);
  196. av_free(s);
  197. return 0;
  198. }
  199. static int tcp_get_file_handle(URLContext *h)
  200. {
  201. TCPContext *s = h->priv_data;
  202. return s->fd;
  203. }
  204. URLProtocol tcp_protocol = {
  205. "tcp",
  206. tcp_open,
  207. tcp_read,
  208. tcp_write,
  209. NULL, /* seek */
  210. tcp_close,
  211. .url_get_file_handle = tcp_get_file_handle,
  212. };