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.

225 lines
6.0KB

  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;
  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. goto redo;
  71. if (ff_neterrno() != FF_NETERROR(EINPROGRESS) &&
  72. ff_neterrno() != FF_NETERROR(EAGAIN))
  73. goto fail;
  74. /* wait until we are connected or until abort */
  75. for(;;) {
  76. if (url_interrupt_cb()) {
  77. ret = AVERROR(EINTR);
  78. goto fail1;
  79. }
  80. fd_max = fd;
  81. FD_ZERO(&wfds);
  82. FD_SET(fd, &wfds);
  83. tv.tv_sec = 0;
  84. tv.tv_usec = 100 * 1000;
  85. ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);
  86. if (ret > 0 && FD_ISSET(fd, &wfds))
  87. break;
  88. }
  89. /* test error */
  90. optlen = sizeof(ret);
  91. getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen);
  92. if (ret != 0) {
  93. av_log(NULL, AV_LOG_ERROR,
  94. "TCP connection to %s:%d failed: %s\n",
  95. hostname, port, strerror(ret));
  96. goto fail;
  97. }
  98. }
  99. s = av_malloc(sizeof(TCPContext));
  100. if (!s) {
  101. freeaddrinfo(ai);
  102. return AVERROR(ENOMEM);
  103. }
  104. h->priv_data = s;
  105. h->is_streamed = 1;
  106. s->fd = fd;
  107. freeaddrinfo(ai);
  108. return 0;
  109. fail:
  110. if (cur_ai->ai_next) {
  111. /* Retry with the next sockaddr */
  112. cur_ai = cur_ai->ai_next;
  113. if (fd >= 0)
  114. closesocket(fd);
  115. goto restart;
  116. }
  117. ret = AVERROR(EIO);
  118. fail1:
  119. if (fd >= 0)
  120. closesocket(fd);
  121. freeaddrinfo(ai);
  122. return ret;
  123. }
  124. static int tcp_read(URLContext *h, uint8_t *buf, int size)
  125. {
  126. TCPContext *s = h->priv_data;
  127. int len, fd_max, ret;
  128. fd_set rfds;
  129. struct timeval tv;
  130. for (;;) {
  131. if (url_interrupt_cb())
  132. return AVERROR(EINTR);
  133. fd_max = s->fd;
  134. FD_ZERO(&rfds);
  135. FD_SET(s->fd, &rfds);
  136. tv.tv_sec = 0;
  137. tv.tv_usec = 100 * 1000;
  138. ret = select(fd_max + 1, &rfds, NULL, NULL, &tv);
  139. if (ret > 0 && FD_ISSET(s->fd, &rfds)) {
  140. len = recv(s->fd, buf, size, 0);
  141. if (len < 0) {
  142. if (ff_neterrno() != FF_NETERROR(EINTR) &&
  143. ff_neterrno() != FF_NETERROR(EAGAIN))
  144. return AVERROR(ff_neterrno());
  145. } else return len;
  146. } else if (ret < 0) {
  147. if (ff_neterrno() == FF_NETERROR(EINTR))
  148. continue;
  149. return -1;
  150. }
  151. }
  152. }
  153. static int tcp_write(URLContext *h, const uint8_t *buf, int size)
  154. {
  155. TCPContext *s = h->priv_data;
  156. int ret, size1, fd_max, len;
  157. fd_set wfds;
  158. struct timeval tv;
  159. size1 = size;
  160. while (size > 0) {
  161. if (url_interrupt_cb())
  162. return AVERROR(EINTR);
  163. fd_max = s->fd;
  164. FD_ZERO(&wfds);
  165. FD_SET(s->fd, &wfds);
  166. tv.tv_sec = 0;
  167. tv.tv_usec = 100 * 1000;
  168. ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);
  169. if (ret > 0 && FD_ISSET(s->fd, &wfds)) {
  170. len = send(s->fd, buf, size, 0);
  171. if (len < 0) {
  172. if (ff_neterrno() != FF_NETERROR(EINTR) &&
  173. ff_neterrno() != FF_NETERROR(EAGAIN))
  174. return AVERROR(ff_neterrno());
  175. continue;
  176. }
  177. size -= len;
  178. buf += len;
  179. } else if (ret < 0) {
  180. if (ff_neterrno() == FF_NETERROR(EINTR))
  181. continue;
  182. return -1;
  183. }
  184. }
  185. return size1 - size;
  186. }
  187. static int tcp_close(URLContext *h)
  188. {
  189. TCPContext *s = h->priv_data;
  190. closesocket(s->fd);
  191. av_free(s);
  192. return 0;
  193. }
  194. static int tcp_get_file_handle(URLContext *h)
  195. {
  196. TCPContext *s = h->priv_data;
  197. return s->fd;
  198. }
  199. URLProtocol tcp_protocol = {
  200. "tcp",
  201. tcp_open,
  202. tcp_read,
  203. tcp_write,
  204. NULL, /* seek */
  205. tcp_close,
  206. .url_get_file_handle = tcp_get_file_handle,
  207. };