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.

239 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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(__APPLE__) || defined(__BEOS__)
  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;
  57. const char *p;
  58. fd_set wfds;
  59. int fd_max, ret;
  60. struct timeval tv;
  61. socklen_t optlen;
  62. s = av_malloc(sizeof(TCPContext));
  63. if (!s)
  64. return -ENOMEM;
  65. h->priv_data = s;
  66. p = uri;
  67. if (!strstart(p, "tcp://", &p))
  68. goto fail;
  69. q = hostname;
  70. while (*p != ':' && *p != '/' && *p != '\0') {
  71. if ((q - hostname) < sizeof(hostname) - 1)
  72. *q++ = *p;
  73. p++;
  74. }
  75. *q = '\0';
  76. if (*p != ':')
  77. goto fail;
  78. p++;
  79. port = strtoul(p, (char **)&p, 10);
  80. if (port <= 0 || port >= 65536)
  81. goto fail;
  82. dest_addr.sin_family = AF_INET;
  83. dest_addr.sin_port = htons(port);
  84. if (resolve_host(&dest_addr.sin_addr, hostname) < 0)
  85. goto fail;
  86. fd = socket(PF_INET, SOCK_STREAM, 0);
  87. if (fd < 0)
  88. goto fail;
  89. fcntl(fd, F_SETFL, O_NONBLOCK);
  90. redo:
  91. ret = connect(fd, (struct sockaddr *)&dest_addr,
  92. sizeof(dest_addr));
  93. if (ret < 0) {
  94. if (errno == EINTR)
  95. goto redo;
  96. if (errno != EINPROGRESS)
  97. goto fail;
  98. /* wait until we are connected or until abort */
  99. for(;;) {
  100. if (url_interrupt_cb()) {
  101. ret = -EINTR;
  102. goto fail1;
  103. }
  104. fd_max = fd;
  105. FD_ZERO(&wfds);
  106. FD_SET(fd, &wfds);
  107. tv.tv_sec = 0;
  108. tv.tv_usec = 100 * 1000;
  109. ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);
  110. if (ret > 0 && FD_ISSET(fd, &wfds))
  111. break;
  112. }
  113. /* test error */
  114. optlen = sizeof(ret);
  115. getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen);
  116. if (ret != 0)
  117. goto fail;
  118. }
  119. s->fd = fd;
  120. return 0;
  121. fail:
  122. ret = -EIO;
  123. fail1:
  124. if (fd >= 0)
  125. close(fd);
  126. av_free(s);
  127. return ret;
  128. }
  129. static int tcp_read(URLContext *h, uint8_t *buf, int size)
  130. {
  131. TCPContext *s = h->priv_data;
  132. int len, fd_max, ret;
  133. fd_set rfds;
  134. struct timeval tv;
  135. for (;;) {
  136. if (url_interrupt_cb())
  137. return -EINTR;
  138. fd_max = s->fd;
  139. FD_ZERO(&rfds);
  140. FD_SET(s->fd, &rfds);
  141. tv.tv_sec = 0;
  142. tv.tv_usec = 100 * 1000;
  143. ret = select(fd_max + 1, &rfds, NULL, NULL, &tv);
  144. if (ret > 0 && FD_ISSET(s->fd, &rfds)) {
  145. #ifdef __BEOS__
  146. len = recv(s->fd, buf, size, 0);
  147. #else
  148. len = read(s->fd, buf, size);
  149. #endif
  150. if (len < 0) {
  151. if (errno != EINTR && errno != EAGAIN)
  152. #ifdef __BEOS__
  153. return errno;
  154. #else
  155. return -errno;
  156. #endif
  157. } else return len;
  158. } else if (ret < 0) {
  159. return -1;
  160. }
  161. }
  162. }
  163. static int tcp_write(URLContext *h, uint8_t *buf, int size)
  164. {
  165. TCPContext *s = h->priv_data;
  166. int ret, size1, fd_max, len;
  167. fd_set wfds;
  168. struct timeval tv;
  169. size1 = size;
  170. while (size > 0) {
  171. if (url_interrupt_cb())
  172. return -EINTR;
  173. fd_max = s->fd;
  174. FD_ZERO(&wfds);
  175. FD_SET(s->fd, &wfds);
  176. tv.tv_sec = 0;
  177. tv.tv_usec = 100 * 1000;
  178. ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);
  179. if (ret > 0 && FD_ISSET(s->fd, &wfds)) {
  180. #ifdef __BEOS__
  181. len = send(s->fd, buf, size, 0);
  182. #else
  183. len = write(s->fd, buf, size);
  184. #endif
  185. if (len < 0) {
  186. if (errno != EINTR && errno != EAGAIN) {
  187. #ifdef __BEOS__
  188. return errno;
  189. #else
  190. return -errno;
  191. #endif
  192. }
  193. continue;
  194. }
  195. size -= len;
  196. buf += len;
  197. } else if (ret < 0) {
  198. return -1;
  199. }
  200. }
  201. return size1 - size;
  202. }
  203. static int tcp_close(URLContext *h)
  204. {
  205. TCPContext *s = h->priv_data;
  206. #ifdef CONFIG_BEOS_NETSERVER
  207. closesocket(s->fd);
  208. #else
  209. close(s->fd);
  210. #endif
  211. av_free(s);
  212. return 0;
  213. }
  214. URLProtocol tcp_protocol = {
  215. "tcp",
  216. tcp_open,
  217. tcp_read,
  218. tcp_write,
  219. NULL, /* seek */
  220. tcp_close,
  221. };