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.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 <sys/time.h>
  25. typedef struct TCPContext {
  26. int fd;
  27. } TCPContext;
  28. /* return non zero if error */
  29. static int tcp_open(URLContext *h, const char *uri, int flags)
  30. {
  31. struct sockaddr_in dest_addr;
  32. char hostname[1024], *q;
  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 proto[1024],path[1024],tmp[1024]; // PETR: protocol and path strings
  40. url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
  41. &port, path, sizeof(path), uri); // PETR: use url_split
  42. if (strcmp(proto,"tcp")) goto fail; // PETR: check protocol
  43. if ((q = strchr(hostname,'@'))) { strcpy(tmp,q+1); strcpy(hostname,tmp); } // PETR: take only the part after '@' for tcp protocol
  44. s = av_malloc(sizeof(TCPContext));
  45. if (!s)
  46. return AVERROR(ENOMEM);
  47. h->priv_data = s;
  48. if (port <= 0 || port >= 65536)
  49. goto fail;
  50. dest_addr.sin_family = AF_INET;
  51. dest_addr.sin_port = htons(port);
  52. if (resolve_host(&dest_addr.sin_addr, hostname) < 0)
  53. goto fail;
  54. fd = socket(AF_INET, SOCK_STREAM, 0);
  55. if (fd < 0)
  56. goto fail;
  57. ff_socket_nonblock(fd, 1);
  58. redo:
  59. ret = connect(fd, (struct sockaddr *)&dest_addr,
  60. sizeof(dest_addr));
  61. if (ret < 0) {
  62. if (ff_neterrno() == FF_NETERROR(EINTR))
  63. goto redo;
  64. if (ff_neterrno() != FF_NETERROR(EINPROGRESS) &&
  65. ff_neterrno() != FF_NETERROR(EAGAIN))
  66. goto fail;
  67. /* wait until we are connected or until abort */
  68. for(;;) {
  69. if (url_interrupt_cb()) {
  70. ret = AVERROR(EINTR);
  71. goto fail1;
  72. }
  73. fd_max = fd;
  74. FD_ZERO(&wfds);
  75. FD_SET(fd, &wfds);
  76. tv.tv_sec = 0;
  77. tv.tv_usec = 100 * 1000;
  78. ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);
  79. if (ret > 0 && FD_ISSET(fd, &wfds))
  80. break;
  81. }
  82. /* test error */
  83. optlen = sizeof(ret);
  84. getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen);
  85. if (ret != 0)
  86. goto fail;
  87. }
  88. s->fd = fd;
  89. return 0;
  90. fail:
  91. ret = AVERROR(EIO);
  92. fail1:
  93. if (fd >= 0)
  94. closesocket(fd);
  95. av_free(s);
  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. av_free(s);
  162. return 0;
  163. }
  164. URLProtocol tcp_protocol = {
  165. "tcp",
  166. tcp_open,
  167. tcp_read,
  168. tcp_write,
  169. NULL, /* seek */
  170. tcp_close,
  171. };