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.

201 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. #include <fcntl.h>
  26. typedef struct TCPContext {
  27. int fd;
  28. } TCPContext;
  29. /* resolve host with also IP address parsing */
  30. int resolve_host(struct in_addr *sin_addr, const char *hostname)
  31. {
  32. struct hostent *hp;
  33. if ((inet_aton(hostname, sin_addr)) == 0) {
  34. hp = gethostbyname(hostname);
  35. if (!hp)
  36. return -1;
  37. memcpy (sin_addr, hp->h_addr, sizeof(struct in_addr));
  38. }
  39. return 0;
  40. }
  41. /* return non zero if error */
  42. static int tcp_open(URLContext *h, const char *uri, int flags)
  43. {
  44. struct sockaddr_in dest_addr;
  45. char hostname[1024], *q;
  46. int port, fd = -1;
  47. TCPContext *s = NULL;
  48. fd_set wfds;
  49. int fd_max, ret;
  50. struct timeval tv;
  51. socklen_t optlen;
  52. char proto[1024],path[1024],tmp[1024]; // PETR: protocol and path strings
  53. url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
  54. &port, path, sizeof(path), uri); // PETR: use url_split
  55. if (strcmp(proto,"tcp")) goto fail; // PETR: check protocol
  56. if ((q = strchr(hostname,'@'))) { strcpy(tmp,q+1); strcpy(hostname,tmp); } // PETR: take only the part after '@' for tcp protocol
  57. s = av_malloc(sizeof(TCPContext));
  58. if (!s)
  59. return AVERROR(ENOMEM);
  60. h->priv_data = s;
  61. if (port <= 0 || port >= 65536)
  62. goto fail;
  63. dest_addr.sin_family = AF_INET;
  64. dest_addr.sin_port = htons(port);
  65. if (resolve_host(&dest_addr.sin_addr, hostname) < 0)
  66. goto fail;
  67. fd = socket(AF_INET, SOCK_STREAM, 0);
  68. if (fd < 0)
  69. goto fail;
  70. fcntl(fd, F_SETFL, O_NONBLOCK);
  71. redo:
  72. ret = connect(fd, (struct sockaddr *)&dest_addr,
  73. sizeof(dest_addr));
  74. if (ret < 0) {
  75. if (errno == EINTR)
  76. goto redo;
  77. if (errno != EINPROGRESS)
  78. goto fail;
  79. /* wait until we are connected or until abort */
  80. for(;;) {
  81. if (url_interrupt_cb()) {
  82. ret = AVERROR(EINTR);
  83. goto fail1;
  84. }
  85. fd_max = fd;
  86. FD_ZERO(&wfds);
  87. FD_SET(fd, &wfds);
  88. tv.tv_sec = 0;
  89. tv.tv_usec = 100 * 1000;
  90. ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);
  91. if (ret > 0 && FD_ISSET(fd, &wfds))
  92. break;
  93. }
  94. /* test error */
  95. optlen = sizeof(ret);
  96. getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen);
  97. if (ret != 0)
  98. goto fail;
  99. }
  100. s->fd = fd;
  101. return 0;
  102. fail:
  103. ret = AVERROR_IO;
  104. fail1:
  105. if (fd >= 0)
  106. closesocket(fd);
  107. av_free(s);
  108. return ret;
  109. }
  110. static int tcp_read(URLContext *h, uint8_t *buf, int size)
  111. {
  112. TCPContext *s = h->priv_data;
  113. int len, fd_max, ret;
  114. fd_set rfds;
  115. struct timeval tv;
  116. for (;;) {
  117. if (url_interrupt_cb())
  118. return AVERROR(EINTR);
  119. fd_max = s->fd;
  120. FD_ZERO(&rfds);
  121. FD_SET(s->fd, &rfds);
  122. tv.tv_sec = 0;
  123. tv.tv_usec = 100 * 1000;
  124. ret = select(fd_max + 1, &rfds, NULL, NULL, &tv);
  125. if (ret > 0 && FD_ISSET(s->fd, &rfds)) {
  126. len = recv(s->fd, buf, size, 0);
  127. if (len < 0) {
  128. if (errno != EINTR && errno != EAGAIN)
  129. return AVERROR(errno);
  130. } else return len;
  131. } else if (ret < 0) {
  132. return -1;
  133. }
  134. }
  135. }
  136. static int tcp_write(URLContext *h, uint8_t *buf, int size)
  137. {
  138. TCPContext *s = h->priv_data;
  139. int ret, size1, fd_max, len;
  140. fd_set wfds;
  141. struct timeval tv;
  142. size1 = size;
  143. while (size > 0) {
  144. if (url_interrupt_cb())
  145. return AVERROR(EINTR);
  146. fd_max = s->fd;
  147. FD_ZERO(&wfds);
  148. FD_SET(s->fd, &wfds);
  149. tv.tv_sec = 0;
  150. tv.tv_usec = 100 * 1000;
  151. ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);
  152. if (ret > 0 && FD_ISSET(s->fd, &wfds)) {
  153. len = send(s->fd, buf, size, 0);
  154. if (len < 0) {
  155. if (errno != EINTR && errno != EAGAIN)
  156. return AVERROR(errno);
  157. continue;
  158. }
  159. size -= len;
  160. buf += len;
  161. } else if (ret < 0) {
  162. return -1;
  163. }
  164. }
  165. return size1 - size;
  166. }
  167. static int tcp_close(URLContext *h)
  168. {
  169. TCPContext *s = h->priv_data;
  170. closesocket(s->fd);
  171. av_free(s);
  172. return 0;
  173. }
  174. URLProtocol tcp_protocol = {
  175. "tcp",
  176. tcp_open,
  177. tcp_read,
  178. tcp_write,
  179. NULL, /* seek */
  180. tcp_close,
  181. };