tcp.c 5.7 KB

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