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.

231 lines
5.7KB

  1. /*
  2. * TCP protocol
  3. * Copyright (c) 2002 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include "avformat.h"
  20. #include <unistd.h>
  21. #include <sys/types.h>
  22. #include <sys/socket.h>
  23. #include <netinet/in.h>
  24. #if defined(__BEOS__) || defined(__INNOTEK_LIBC__)
  25. typedef int socklen_t;
  26. #endif
  27. #ifndef __BEOS__
  28. # include <arpa/inet.h>
  29. #else
  30. # include "barpainet.h"
  31. #endif
  32. #include <netdb.h>
  33. #include <sys/time.h>
  34. #include <fcntl.h>
  35. typedef struct TCPContext {
  36. int fd;
  37. } TCPContext;
  38. /* resolve host with also IP address parsing */
  39. int resolve_host(struct in_addr *sin_addr, const char *hostname)
  40. {
  41. struct hostent *hp;
  42. if ((inet_aton(hostname, sin_addr)) == 0) {
  43. hp = gethostbyname(hostname);
  44. if (!hp)
  45. return -1;
  46. memcpy (sin_addr, hp->h_addr, sizeof(struct in_addr));
  47. }
  48. return 0;
  49. }
  50. /* return non zero if error */
  51. static int tcp_open(URLContext *h, const char *uri, int flags)
  52. {
  53. struct sockaddr_in dest_addr;
  54. char hostname[1024], *q;
  55. int port, fd = -1;
  56. TCPContext *s = NULL;
  57. fd_set wfds;
  58. int fd_max, ret;
  59. struct timeval tv;
  60. socklen_t optlen;
  61. char proto[1024],path[1024],tmp[1024]; // PETR: protocol and path strings
  62. url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
  63. &port, path, sizeof(path), uri); // PETR: use url_split
  64. if (strcmp(proto,"tcp")) goto fail; // PETR: check protocol
  65. if ((q = strchr(hostname,'@'))) { strcpy(tmp,q+1); strcpy(hostname,tmp); } // PETR: take only the part after '@' for tcp protocol
  66. s = av_malloc(sizeof(TCPContext));
  67. if (!s)
  68. return -ENOMEM;
  69. h->priv_data = s;
  70. if (port <= 0 || port >= 65536)
  71. goto fail;
  72. dest_addr.sin_family = AF_INET;
  73. dest_addr.sin_port = htons(port);
  74. if (resolve_host(&dest_addr.sin_addr, hostname) < 0)
  75. goto fail;
  76. fd = socket(PF_INET, SOCK_STREAM, 0);
  77. if (fd < 0)
  78. goto fail;
  79. fcntl(fd, F_SETFL, O_NONBLOCK);
  80. redo:
  81. ret = connect(fd, (struct sockaddr *)&dest_addr,
  82. sizeof(dest_addr));
  83. if (ret < 0) {
  84. if (errno == EINTR)
  85. goto redo;
  86. if (errno != EINPROGRESS)
  87. goto fail;
  88. /* wait until we are connected or until abort */
  89. for(;;) {
  90. if (url_interrupt_cb()) {
  91. ret = -EINTR;
  92. goto fail1;
  93. }
  94. fd_max = fd;
  95. FD_ZERO(&wfds);
  96. FD_SET(fd, &wfds);
  97. tv.tv_sec = 0;
  98. tv.tv_usec = 100 * 1000;
  99. ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);
  100. if (ret > 0 && FD_ISSET(fd, &wfds))
  101. break;
  102. }
  103. /* test error */
  104. optlen = sizeof(ret);
  105. getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen);
  106. if (ret != 0)
  107. goto fail;
  108. }
  109. s->fd = fd;
  110. return 0;
  111. fail:
  112. ret = AVERROR_IO;
  113. fail1:
  114. if (fd >= 0)
  115. close(fd);
  116. av_free(s);
  117. return ret;
  118. }
  119. static int tcp_read(URLContext *h, uint8_t *buf, int size)
  120. {
  121. TCPContext *s = h->priv_data;
  122. int len, fd_max, ret;
  123. fd_set rfds;
  124. struct timeval tv;
  125. for (;;) {
  126. if (url_interrupt_cb())
  127. return -EINTR;
  128. fd_max = s->fd;
  129. FD_ZERO(&rfds);
  130. FD_SET(s->fd, &rfds);
  131. tv.tv_sec = 0;
  132. tv.tv_usec = 100 * 1000;
  133. ret = select(fd_max + 1, &rfds, NULL, NULL, &tv);
  134. if (ret > 0 && FD_ISSET(s->fd, &rfds)) {
  135. #ifdef __BEOS__
  136. len = recv(s->fd, buf, size, 0);
  137. #else
  138. len = read(s->fd, buf, size);
  139. #endif
  140. if (len < 0) {
  141. if (errno != EINTR && errno != EAGAIN)
  142. #ifdef __BEOS__
  143. return errno;
  144. #else
  145. return -errno;
  146. #endif
  147. } else return len;
  148. } else if (ret < 0) {
  149. return -1;
  150. }
  151. }
  152. }
  153. static int tcp_write(URLContext *h, uint8_t *buf, int size)
  154. {
  155. TCPContext *s = h->priv_data;
  156. int ret, size1, fd_max, len;
  157. fd_set wfds;
  158. struct timeval tv;
  159. size1 = size;
  160. while (size > 0) {
  161. if (url_interrupt_cb())
  162. return -EINTR;
  163. fd_max = s->fd;
  164. FD_ZERO(&wfds);
  165. FD_SET(s->fd, &wfds);
  166. tv.tv_sec = 0;
  167. tv.tv_usec = 100 * 1000;
  168. ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);
  169. if (ret > 0 && FD_ISSET(s->fd, &wfds)) {
  170. #ifdef __BEOS__
  171. len = send(s->fd, buf, size, 0);
  172. #else
  173. len = write(s->fd, buf, size);
  174. #endif
  175. if (len < 0) {
  176. if (errno != EINTR && errno != EAGAIN) {
  177. #ifdef __BEOS__
  178. return errno;
  179. #else
  180. return -errno;
  181. #endif
  182. }
  183. continue;
  184. }
  185. size -= len;
  186. buf += len;
  187. } else if (ret < 0) {
  188. return -1;
  189. }
  190. }
  191. return size1 - size;
  192. }
  193. static int tcp_close(URLContext *h)
  194. {
  195. TCPContext *s = h->priv_data;
  196. #ifdef CONFIG_BEOS_NETSERVER
  197. closesocket(s->fd);
  198. #else
  199. close(s->fd);
  200. #endif
  201. av_free(s);
  202. return 0;
  203. }
  204. URLProtocol tcp_protocol = {
  205. "tcp",
  206. tcp_open,
  207. tcp_read,
  208. tcp_write,
  209. NULL, /* seek */
  210. tcp_close,
  211. };