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.

195 lines
5.2KB

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