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.

215 lines
5.6KB

  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 "os_support.h"
  25. #if HAVE_SYS_SELECT_H
  26. #include <sys/select.h>
  27. #endif
  28. #include <sys/time.h>
  29. typedef struct TCPContext {
  30. int fd;
  31. } TCPContext;
  32. /* return non zero if error */
  33. static int tcp_open(URLContext *h, const char *uri, int flags)
  34. {
  35. struct addrinfo hints, *ai, *cur_ai;
  36. int port, fd = -1;
  37. TCPContext *s = NULL;
  38. fd_set wfds;
  39. int fd_max, ret;
  40. struct timeval tv;
  41. socklen_t optlen;
  42. char hostname[1024],proto[1024],path[1024];
  43. char portstr[10];
  44. if(!ff_network_init())
  45. return AVERROR(EIO);
  46. url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
  47. &port, path, sizeof(path), uri);
  48. if (strcmp(proto,"tcp") || port <= 0 || port >= 65536)
  49. return AVERROR(EINVAL);
  50. memset(&hints, 0, sizeof(hints));
  51. hints.ai_family = AF_UNSPEC;
  52. hints.ai_socktype = SOCK_STREAM;
  53. snprintf(portstr, sizeof(portstr), "%d", port);
  54. if (getaddrinfo(hostname, portstr, &hints, &ai))
  55. return AVERROR(EIO);
  56. cur_ai = ai;
  57. restart:
  58. fd = socket(cur_ai->ai_family, cur_ai->ai_socktype, cur_ai->ai_protocol);
  59. if (fd < 0)
  60. goto fail;
  61. ff_socket_nonblock(fd, 1);
  62. redo:
  63. ret = connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
  64. if (ret < 0) {
  65. if (ff_neterrno() == FF_NETERROR(EINTR))
  66. goto redo;
  67. if (ff_neterrno() != FF_NETERROR(EINPROGRESS) &&
  68. ff_neterrno() != FF_NETERROR(EAGAIN))
  69. goto fail;
  70. /* wait until we are connected or until abort */
  71. for(;;) {
  72. if (url_interrupt_cb()) {
  73. ret = AVERROR(EINTR);
  74. goto fail1;
  75. }
  76. fd_max = fd;
  77. FD_ZERO(&wfds);
  78. FD_SET(fd, &wfds);
  79. tv.tv_sec = 0;
  80. tv.tv_usec = 100 * 1000;
  81. ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);
  82. if (ret > 0 && FD_ISSET(fd, &wfds))
  83. break;
  84. }
  85. /* test error */
  86. optlen = sizeof(ret);
  87. getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen);
  88. if (ret != 0)
  89. goto fail;
  90. }
  91. s = av_malloc(sizeof(TCPContext));
  92. if (!s) {
  93. freeaddrinfo(ai);
  94. return AVERROR(ENOMEM);
  95. }
  96. h->priv_data = s;
  97. h->is_streamed = 1;
  98. s->fd = fd;
  99. freeaddrinfo(ai);
  100. return 0;
  101. fail:
  102. if (cur_ai->ai_next) {
  103. /* Retry with the next sockaddr */
  104. cur_ai = cur_ai->ai_next;
  105. if (fd >= 0)
  106. closesocket(fd);
  107. goto restart;
  108. }
  109. ret = AVERROR(EIO);
  110. fail1:
  111. if (fd >= 0)
  112. closesocket(fd);
  113. freeaddrinfo(ai);
  114. return ret;
  115. }
  116. static int tcp_read(URLContext *h, uint8_t *buf, int size)
  117. {
  118. TCPContext *s = h->priv_data;
  119. int len, fd_max, ret;
  120. fd_set rfds;
  121. struct timeval tv;
  122. for (;;) {
  123. if (url_interrupt_cb())
  124. return AVERROR(EINTR);
  125. fd_max = s->fd;
  126. FD_ZERO(&rfds);
  127. FD_SET(s->fd, &rfds);
  128. tv.tv_sec = 0;
  129. tv.tv_usec = 100 * 1000;
  130. ret = select(fd_max + 1, &rfds, NULL, NULL, &tv);
  131. if (ret > 0 && FD_ISSET(s->fd, &rfds)) {
  132. len = recv(s->fd, buf, size, 0);
  133. if (len < 0) {
  134. if (ff_neterrno() != FF_NETERROR(EINTR) &&
  135. ff_neterrno() != FF_NETERROR(EAGAIN))
  136. return AVERROR(ff_neterrno());
  137. } else return len;
  138. } else if (ret < 0) {
  139. return -1;
  140. }
  141. }
  142. }
  143. static int tcp_write(URLContext *h, uint8_t *buf, int size)
  144. {
  145. TCPContext *s = h->priv_data;
  146. int ret, size1, fd_max, len;
  147. fd_set wfds;
  148. struct timeval tv;
  149. size1 = size;
  150. while (size > 0) {
  151. if (url_interrupt_cb())
  152. return AVERROR(EINTR);
  153. fd_max = s->fd;
  154. FD_ZERO(&wfds);
  155. FD_SET(s->fd, &wfds);
  156. tv.tv_sec = 0;
  157. tv.tv_usec = 100 * 1000;
  158. ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);
  159. if (ret > 0 && FD_ISSET(s->fd, &wfds)) {
  160. len = send(s->fd, buf, size, 0);
  161. if (len < 0) {
  162. if (ff_neterrno() != FF_NETERROR(EINTR) &&
  163. ff_neterrno() != FF_NETERROR(EAGAIN))
  164. return AVERROR(ff_neterrno());
  165. continue;
  166. }
  167. size -= len;
  168. buf += len;
  169. } else if (ret < 0) {
  170. return -1;
  171. }
  172. }
  173. return size1 - size;
  174. }
  175. static int tcp_close(URLContext *h)
  176. {
  177. TCPContext *s = h->priv_data;
  178. closesocket(s->fd);
  179. ff_network_close();
  180. av_free(s);
  181. return 0;
  182. }
  183. static int tcp_get_file_handle(URLContext *h)
  184. {
  185. TCPContext *s = h->priv_data;
  186. return s->fd;
  187. }
  188. URLProtocol tcp_protocol = {
  189. "tcp",
  190. tcp_open,
  191. tcp_read,
  192. tcp_write,
  193. NULL, /* seek */
  194. tcp_close,
  195. .url_get_file_handle = tcp_get_file_handle,
  196. };