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.

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