tcp.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 sockaddr_in dest_addr;
  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. if(!ff_network_init())
  44. return AVERROR(EIO);
  45. 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. dest_addr.sin_family = AF_INET;
  50. dest_addr.sin_port = htons(port);
  51. if (resolve_host(&dest_addr.sin_addr, hostname) < 0)
  52. return AVERROR(EIO);
  53. fd = socket(AF_INET, SOCK_STREAM, 0);
  54. if (fd < 0)
  55. return AVERROR(EIO);
  56. ff_socket_nonblock(fd, 1);
  57. redo:
  58. ret = connect(fd, (struct sockaddr *)&dest_addr,
  59. sizeof(dest_addr));
  60. if (ret < 0) {
  61. if (ff_neterrno() == FF_NETERROR(EINTR))
  62. goto redo;
  63. if (ff_neterrno() != FF_NETERROR(EINPROGRESS) &&
  64. ff_neterrno() != FF_NETERROR(EAGAIN))
  65. goto fail;
  66. /* wait until we are connected or until abort */
  67. for(;;) {
  68. if (url_interrupt_cb()) {
  69. ret = AVERROR(EINTR);
  70. goto fail1;
  71. }
  72. fd_max = fd;
  73. FD_ZERO(&wfds);
  74. FD_SET(fd, &wfds);
  75. tv.tv_sec = 0;
  76. tv.tv_usec = 100 * 1000;
  77. ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);
  78. if (ret > 0 && FD_ISSET(fd, &wfds))
  79. break;
  80. }
  81. /* test error */
  82. optlen = sizeof(ret);
  83. getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen);
  84. if (ret != 0)
  85. goto fail;
  86. }
  87. s = av_malloc(sizeof(TCPContext));
  88. if (!s)
  89. return AVERROR(ENOMEM);
  90. h->priv_data = s;
  91. h->is_streamed = 1;
  92. s->fd = fd;
  93. return 0;
  94. fail:
  95. ret = AVERROR(EIO);
  96. fail1:
  97. if (fd >= 0)
  98. closesocket(fd);
  99. return ret;
  100. }
  101. static int tcp_read(URLContext *h, uint8_t *buf, int size)
  102. {
  103. TCPContext *s = h->priv_data;
  104. int len, fd_max, ret;
  105. fd_set rfds;
  106. struct timeval tv;
  107. for (;;) {
  108. if (url_interrupt_cb())
  109. return AVERROR(EINTR);
  110. fd_max = s->fd;
  111. FD_ZERO(&rfds);
  112. FD_SET(s->fd, &rfds);
  113. tv.tv_sec = 0;
  114. tv.tv_usec = 100 * 1000;
  115. ret = select(fd_max + 1, &rfds, NULL, NULL, &tv);
  116. if (ret > 0 && FD_ISSET(s->fd, &rfds)) {
  117. len = recv(s->fd, buf, size, 0);
  118. if (len < 0) {
  119. if (ff_neterrno() != FF_NETERROR(EINTR) &&
  120. ff_neterrno() != FF_NETERROR(EAGAIN))
  121. return AVERROR(errno);
  122. } else return len;
  123. } else if (ret < 0) {
  124. return -1;
  125. }
  126. }
  127. }
  128. static int tcp_write(URLContext *h, uint8_t *buf, int size)
  129. {
  130. TCPContext *s = h->priv_data;
  131. int ret, size1, fd_max, len;
  132. fd_set wfds;
  133. struct timeval tv;
  134. size1 = size;
  135. while (size > 0) {
  136. if (url_interrupt_cb())
  137. return AVERROR(EINTR);
  138. fd_max = s->fd;
  139. FD_ZERO(&wfds);
  140. FD_SET(s->fd, &wfds);
  141. tv.tv_sec = 0;
  142. tv.tv_usec = 100 * 1000;
  143. ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);
  144. if (ret > 0 && FD_ISSET(s->fd, &wfds)) {
  145. len = send(s->fd, buf, size, 0);
  146. if (len < 0) {
  147. if (ff_neterrno() != FF_NETERROR(EINTR) &&
  148. ff_neterrno() != FF_NETERROR(EAGAIN))
  149. return AVERROR(errno);
  150. continue;
  151. }
  152. size -= len;
  153. buf += len;
  154. } else if (ret < 0) {
  155. return -1;
  156. }
  157. }
  158. return size1 - size;
  159. }
  160. static int tcp_close(URLContext *h)
  161. {
  162. TCPContext *s = h->priv_data;
  163. closesocket(s->fd);
  164. ff_network_close();
  165. av_free(s);
  166. return 0;
  167. }
  168. URLProtocol tcp_protocol = {
  169. "tcp",
  170. tcp_open,
  171. tcp_read,
  172. tcp_write,
  173. NULL, /* seek */
  174. tcp_close,
  175. };