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.

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