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.

187 lines
4.9KB

  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. #include <fcntl.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. dest_addr.sin_family = AF_INET;
  52. dest_addr.sin_port = htons(port);
  53. if (resolve_host(&dest_addr.sin_addr, hostname) < 0)
  54. goto fail;
  55. fd = socket(AF_INET, SOCK_STREAM, 0);
  56. if (fd < 0)
  57. goto fail;
  58. fcntl(fd, F_SETFL, O_NONBLOCK);
  59. redo:
  60. ret = connect(fd, (struct sockaddr *)&dest_addr,
  61. sizeof(dest_addr));
  62. if (ret < 0) {
  63. if (errno == EINTR)
  64. goto redo;
  65. if (errno != EINPROGRESS)
  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_IO;
  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 (errno != EINTR && errno != 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 (errno != EINTR && errno != EAGAIN)
  144. return AVERROR(errno);
  145. continue;
  146. }
  147. size -= len;
  148. buf += len;
  149. } else if (ret < 0) {
  150. return -1;
  151. }
  152. }
  153. return size1 - size;
  154. }
  155. static int tcp_close(URLContext *h)
  156. {
  157. TCPContext *s = h->priv_data;
  158. closesocket(s->fd);
  159. av_free(s);
  160. return 0;
  161. }
  162. URLProtocol tcp_protocol = {
  163. "tcp",
  164. tcp_open,
  165. tcp_read,
  166. tcp_write,
  167. NULL, /* seek */
  168. tcp_close,
  169. };