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.

188 lines
5.0KB

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