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.

233 lines
5.8KB

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