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.

214 lines
5.4KB

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