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.

232 lines
5.8KB

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