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.

211 lines
5.5KB

  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. ff_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
  45. &port, path, sizeof(path), uri);
  46. if (strcmp(proto,"tcp") || port <= 0 || port >= 65536)
  47. return AVERROR(EINVAL);
  48. memset(&hints, 0, sizeof(hints));
  49. hints.ai_family = AF_UNSPEC;
  50. hints.ai_socktype = SOCK_STREAM;
  51. snprintf(portstr, sizeof(portstr), "%d", port);
  52. if (getaddrinfo(hostname, portstr, &hints, &ai))
  53. return AVERROR(EIO);
  54. cur_ai = ai;
  55. restart:
  56. fd = socket(cur_ai->ai_family, cur_ai->ai_socktype, cur_ai->ai_protocol);
  57. if (fd < 0)
  58. goto fail;
  59. ff_socket_nonblock(fd, 1);
  60. redo:
  61. ret = connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
  62. if (ret < 0) {
  63. if (ff_neterrno() == FF_NETERROR(EINTR))
  64. goto redo;
  65. if (ff_neterrno() != FF_NETERROR(EINPROGRESS) &&
  66. ff_neterrno() != FF_NETERROR(EAGAIN))
  67. goto fail;
  68. /* wait until we are connected or until abort */
  69. for(;;) {
  70. if (url_interrupt_cb()) {
  71. ret = AVERROR(EINTR);
  72. goto fail1;
  73. }
  74. fd_max = fd;
  75. FD_ZERO(&wfds);
  76. FD_SET(fd, &wfds);
  77. tv.tv_sec = 0;
  78. tv.tv_usec = 100 * 1000;
  79. ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);
  80. if (ret > 0 && FD_ISSET(fd, &wfds))
  81. break;
  82. }
  83. /* test error */
  84. optlen = sizeof(ret);
  85. getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen);
  86. if (ret != 0)
  87. goto fail;
  88. }
  89. s = av_malloc(sizeof(TCPContext));
  90. if (!s) {
  91. freeaddrinfo(ai);
  92. return AVERROR(ENOMEM);
  93. }
  94. h->priv_data = s;
  95. h->is_streamed = 1;
  96. s->fd = fd;
  97. freeaddrinfo(ai);
  98. return 0;
  99. fail:
  100. if (cur_ai->ai_next) {
  101. /* Retry with the next sockaddr */
  102. cur_ai = cur_ai->ai_next;
  103. if (fd >= 0)
  104. closesocket(fd);
  105. goto restart;
  106. }
  107. ret = AVERROR(EIO);
  108. fail1:
  109. if (fd >= 0)
  110. closesocket(fd);
  111. freeaddrinfo(ai);
  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 AVERROR(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 (ff_neterrno() != FF_NETERROR(EINTR) &&
  133. ff_neterrno() != FF_NETERROR(EAGAIN))
  134. return AVERROR(ff_neterrno());
  135. } else return len;
  136. } else if (ret < 0) {
  137. return -1;
  138. }
  139. }
  140. }
  141. static int tcp_write(URLContext *h, uint8_t *buf, int size)
  142. {
  143. TCPContext *s = h->priv_data;
  144. int ret, size1, fd_max, len;
  145. fd_set wfds;
  146. struct timeval tv;
  147. size1 = size;
  148. while (size > 0) {
  149. if (url_interrupt_cb())
  150. return AVERROR(EINTR);
  151. fd_max = s->fd;
  152. FD_ZERO(&wfds);
  153. FD_SET(s->fd, &wfds);
  154. tv.tv_sec = 0;
  155. tv.tv_usec = 100 * 1000;
  156. ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);
  157. if (ret > 0 && FD_ISSET(s->fd, &wfds)) {
  158. len = send(s->fd, buf, size, 0);
  159. if (len < 0) {
  160. if (ff_neterrno() != FF_NETERROR(EINTR) &&
  161. ff_neterrno() != FF_NETERROR(EAGAIN))
  162. return AVERROR(ff_neterrno());
  163. continue;
  164. }
  165. size -= len;
  166. buf += len;
  167. } else if (ret < 0) {
  168. return -1;
  169. }
  170. }
  171. return size1 - size;
  172. }
  173. static int tcp_close(URLContext *h)
  174. {
  175. TCPContext *s = h->priv_data;
  176. closesocket(s->fd);
  177. av_free(s);
  178. return 0;
  179. }
  180. static int tcp_get_file_handle(URLContext *h)
  181. {
  182. TCPContext *s = h->priv_data;
  183. return s->fd;
  184. }
  185. URLProtocol tcp_protocol = {
  186. "tcp",
  187. tcp_open,
  188. tcp_read,
  189. tcp_write,
  190. NULL, /* seek */
  191. tcp_close,
  192. .url_get_file_handle = tcp_get_file_handle,
  193. };