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.

177 lines
3.9KB

  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. typedef struct TCPContext {
  32. int fd;
  33. } TCPContext;
  34. /* resolve host with also IP address parsing */
  35. int resolve_host(struct in_addr *sin_addr, const char *hostname)
  36. {
  37. struct hostent *hp;
  38. if ((inet_aton(hostname, sin_addr)) == 0) {
  39. hp = gethostbyname(hostname);
  40. if (!hp)
  41. return -1;
  42. memcpy (sin_addr, hp->h_addr, sizeof(struct in_addr));
  43. }
  44. return 0;
  45. }
  46. /* return non zero if error */
  47. static int tcp_open(URLContext *h, const char *uri, int flags)
  48. {
  49. struct sockaddr_in dest_addr;
  50. char hostname[1024], *q;
  51. int port, fd = -1;
  52. TCPContext *s;
  53. const char *p;
  54. s = av_malloc(sizeof(TCPContext));
  55. if (!s)
  56. return -ENOMEM;
  57. h->priv_data = s;
  58. p = uri;
  59. if (!strstart(p, "tcp://", &p))
  60. goto fail;
  61. q = hostname;
  62. while (*p != ':' && *p != '/' && *p != '\0') {
  63. if ((q - hostname) < sizeof(hostname) - 1)
  64. *q++ = *p;
  65. p++;
  66. }
  67. *q = '\0';
  68. if (*p != ':')
  69. goto fail;
  70. p++;
  71. port = strtoul(p, (char **)&p, 10);
  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. if (connect(fd, (struct sockaddr *)&dest_addr,
  82. sizeof(dest_addr)) < 0)
  83. goto fail;
  84. s->fd = fd;
  85. return 0;
  86. fail:
  87. if (fd >= 0)
  88. close(fd);
  89. av_free(s);
  90. return -EIO;
  91. }
  92. static int tcp_read(URLContext *h, UINT8 *buf, int size)
  93. {
  94. TCPContext *s = h->priv_data;
  95. int size1, len;
  96. size1 = size;
  97. while (size > 0) {
  98. #ifdef CONFIG_BEOS_NETSERVER
  99. len = recv (s->fd, buf, size, 0);
  100. #else
  101. len = read (s->fd, buf, size);
  102. #endif
  103. if (len < 0) {
  104. if (errno != EINTR && errno != EAGAIN)
  105. #ifdef __BEOS__
  106. return errno;
  107. #else
  108. return -errno;
  109. #endif
  110. else
  111. continue;
  112. } else if (len == 0) {
  113. break;
  114. }
  115. size -= len;
  116. buf += len;
  117. }
  118. return size1 - size;
  119. }
  120. static int tcp_write(URLContext *h, UINT8 *buf, int size)
  121. {
  122. TCPContext *s = h->priv_data;
  123. int ret, size1;
  124. size1 = size;
  125. while (size > 0) {
  126. #ifdef CONFIG_BEOS_NETSERVER
  127. ret = send (s->fd, buf, size, 0);
  128. #else
  129. ret = write (s->fd, buf, size);
  130. #endif
  131. if (ret < 0 && errno != EINTR && errno != EAGAIN)
  132. #ifdef __BEOS__
  133. return errno;
  134. #else
  135. return -errno;
  136. #endif
  137. size -= ret;
  138. buf += ret;
  139. }
  140. return size1 - size;
  141. }
  142. static int tcp_close(URLContext *h)
  143. {
  144. TCPContext *s = h->priv_data;
  145. #ifdef CONFIG_BEOS_NETSERVER
  146. closesocket(s->fd);
  147. #else
  148. close(s->fd);
  149. #endif
  150. av_free(s);
  151. return 0;
  152. }
  153. URLProtocol tcp_protocol = {
  154. "tcp",
  155. tcp_open,
  156. tcp_read,
  157. tcp_write,
  158. NULL, /* seek */
  159. tcp_close,
  160. };