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.

210 lines
5.3KB

  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 -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(PF_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 = -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 -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. #ifdef __BEOS__
  130. return errno;
  131. #else
  132. return -errno;
  133. #endif
  134. } else return len;
  135. } else if (ret < 0) {
  136. return -1;
  137. }
  138. }
  139. }
  140. static int tcp_write(URLContext *h, uint8_t *buf, int size)
  141. {
  142. TCPContext *s = h->priv_data;
  143. int ret, size1, fd_max, len;
  144. fd_set wfds;
  145. struct timeval tv;
  146. size1 = size;
  147. while (size > 0) {
  148. if (url_interrupt_cb())
  149. return -EINTR;
  150. fd_max = s->fd;
  151. FD_ZERO(&wfds);
  152. FD_SET(s->fd, &wfds);
  153. tv.tv_sec = 0;
  154. tv.tv_usec = 100 * 1000;
  155. ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);
  156. if (ret > 0 && FD_ISSET(s->fd, &wfds)) {
  157. len = send(s->fd, buf, size, 0);
  158. if (len < 0) {
  159. if (errno != EINTR && errno != EAGAIN) {
  160. #ifdef __BEOS__
  161. return errno;
  162. #else
  163. return -errno;
  164. #endif
  165. }
  166. continue;
  167. }
  168. size -= len;
  169. buf += len;
  170. } else if (ret < 0) {
  171. return -1;
  172. }
  173. }
  174. return size1 - size;
  175. }
  176. static int tcp_close(URLContext *h)
  177. {
  178. TCPContext *s = h->priv_data;
  179. closesocket(s->fd);
  180. av_free(s);
  181. return 0;
  182. }
  183. URLProtocol tcp_protocol = {
  184. "tcp",
  185. tcp_open,
  186. tcp_read,
  187. tcp_write,
  188. NULL, /* seek */
  189. tcp_close,
  190. };