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.

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