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.

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