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.

189 lines
5.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 "network.h"
  24. #include "os_support.h"
  25. #include <sys/time.h>
  26. typedef struct TCPContext {
  27. int fd;
  28. } TCPContext;
  29. /* return non zero if error */
  30. static int tcp_open(URLContext *h, const char *uri, int flags)
  31. {
  32. struct sockaddr_in dest_addr;
  33. int port, fd = -1;
  34. TCPContext *s = NULL;
  35. fd_set wfds;
  36. int fd_max, ret;
  37. struct timeval tv;
  38. socklen_t optlen;
  39. char hostname[1024],proto[1024],path[1024];
  40. if(!ff_network_init())
  41. return AVERROR(EIO);
  42. url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
  43. &port, path, sizeof(path), uri);
  44. if (strcmp(proto,"tcp") || port <= 0 || port >= 65536)
  45. return AVERROR(EINVAL);
  46. dest_addr.sin_family = AF_INET;
  47. dest_addr.sin_port = htons(port);
  48. if (resolve_host(&dest_addr.sin_addr, hostname) < 0)
  49. return AVERROR(EIO);
  50. fd = socket(AF_INET, SOCK_STREAM, 0);
  51. if (fd < 0)
  52. return AVERROR(EIO);
  53. ff_socket_nonblock(fd, 1);
  54. redo:
  55. ret = connect(fd, (struct sockaddr *)&dest_addr,
  56. sizeof(dest_addr));
  57. if (ret < 0) {
  58. if (ff_neterrno() == FF_NETERROR(EINTR))
  59. goto redo;
  60. if (ff_neterrno() != FF_NETERROR(EINPROGRESS) &&
  61. ff_neterrno() != FF_NETERROR(EAGAIN))
  62. goto fail;
  63. /* wait until we are connected or until abort */
  64. for(;;) {
  65. if (url_interrupt_cb()) {
  66. ret = AVERROR(EINTR);
  67. goto fail1;
  68. }
  69. fd_max = fd;
  70. FD_ZERO(&wfds);
  71. FD_SET(fd, &wfds);
  72. tv.tv_sec = 0;
  73. tv.tv_usec = 100 * 1000;
  74. ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);
  75. if (ret > 0 && FD_ISSET(fd, &wfds))
  76. break;
  77. }
  78. /* test error */
  79. optlen = sizeof(ret);
  80. getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen);
  81. if (ret != 0)
  82. goto fail;
  83. }
  84. s = av_malloc(sizeof(TCPContext));
  85. if (!s)
  86. return AVERROR(ENOMEM);
  87. h->priv_data = s;
  88. h->is_streamed = 1;
  89. s->fd = fd;
  90. return 0;
  91. fail:
  92. ret = AVERROR(EIO);
  93. fail1:
  94. if (fd >= 0)
  95. closesocket(fd);
  96. return ret;
  97. }
  98. static int tcp_read(URLContext *h, uint8_t *buf, int size)
  99. {
  100. TCPContext *s = h->priv_data;
  101. int len, fd_max, ret;
  102. fd_set rfds;
  103. struct timeval tv;
  104. for (;;) {
  105. if (url_interrupt_cb())
  106. return AVERROR(EINTR);
  107. fd_max = s->fd;
  108. FD_ZERO(&rfds);
  109. FD_SET(s->fd, &rfds);
  110. tv.tv_sec = 0;
  111. tv.tv_usec = 100 * 1000;
  112. ret = select(fd_max + 1, &rfds, NULL, NULL, &tv);
  113. if (ret > 0 && FD_ISSET(s->fd, &rfds)) {
  114. len = recv(s->fd, buf, size, 0);
  115. if (len < 0) {
  116. if (ff_neterrno() != FF_NETERROR(EINTR) &&
  117. ff_neterrno() != FF_NETERROR(EAGAIN))
  118. return AVERROR(errno);
  119. } else return len;
  120. } else if (ret < 0) {
  121. return -1;
  122. }
  123. }
  124. }
  125. static int tcp_write(URLContext *h, uint8_t *buf, int size)
  126. {
  127. TCPContext *s = h->priv_data;
  128. int ret, size1, fd_max, len;
  129. fd_set wfds;
  130. struct timeval tv;
  131. size1 = size;
  132. while (size > 0) {
  133. if (url_interrupt_cb())
  134. return AVERROR(EINTR);
  135. fd_max = s->fd;
  136. FD_ZERO(&wfds);
  137. FD_SET(s->fd, &wfds);
  138. tv.tv_sec = 0;
  139. tv.tv_usec = 100 * 1000;
  140. ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);
  141. if (ret > 0 && FD_ISSET(s->fd, &wfds)) {
  142. len = send(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(errno);
  147. continue;
  148. }
  149. size -= len;
  150. buf += len;
  151. } else if (ret < 0) {
  152. return -1;
  153. }
  154. }
  155. return size1 - size;
  156. }
  157. static int tcp_close(URLContext *h)
  158. {
  159. TCPContext *s = h->priv_data;
  160. closesocket(s->fd);
  161. ff_network_close();
  162. av_free(s);
  163. return 0;
  164. }
  165. URLProtocol tcp_protocol = {
  166. "tcp",
  167. tcp_open,
  168. tcp_read,
  169. tcp_write,
  170. NULL, /* seek */
  171. tcp_close,
  172. };