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.4KB

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