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.

153 lines
3.5KB

  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. #include <arpa/inet.h>
  26. #include <netdb.h>
  27. typedef struct TCPContext {
  28. int fd;
  29. } TCPContext;
  30. /* resolve host with also IP address parsing */
  31. int resolve_host(struct in_addr *sin_addr, const char *hostname)
  32. {
  33. struct hostent *hp;
  34. if ((inet_aton(hostname, sin_addr)) == 0) {
  35. hp = gethostbyname(hostname);
  36. if (!hp)
  37. return -1;
  38. memcpy (sin_addr, hp->h_addr, sizeof(struct in_addr));
  39. }
  40. return 0;
  41. }
  42. /* return non zero if error */
  43. static int tcp_open(URLContext *h, const char *uri, int flags)
  44. {
  45. struct sockaddr_in dest_addr;
  46. char hostname[1024], *q;
  47. int port, fd = -1;
  48. TCPContext *s;
  49. const char *p;
  50. s = av_malloc(sizeof(TCPContext));
  51. if (!s)
  52. return -ENOMEM;
  53. h->priv_data = s;
  54. p = uri;
  55. if (!strstart(p, "tcp://", &p))
  56. goto fail;
  57. q = hostname;
  58. while (*p != ':' && *p != '/' && *p != '\0') {
  59. if ((q - hostname) < sizeof(hostname) - 1)
  60. *q++ = *p;
  61. p++;
  62. }
  63. *q = '\0';
  64. if (*p != ':')
  65. goto fail;
  66. p++;
  67. port = strtoul(p, (char **)&p, 10);
  68. if (port <= 0 || port >= 65536)
  69. goto fail;
  70. dest_addr.sin_family = AF_INET;
  71. dest_addr.sin_port = htons(port);
  72. if (resolve_host(&dest_addr.sin_addr, hostname) < 0)
  73. goto fail;
  74. fd = socket(PF_INET, SOCK_STREAM, 0);
  75. if (fd < 0)
  76. goto fail;
  77. if (connect(fd, (struct sockaddr *)&dest_addr,
  78. sizeof(dest_addr)) < 0)
  79. goto fail;
  80. s->fd = fd;
  81. return 0;
  82. fail:
  83. if (fd >= 0)
  84. close(fd);
  85. av_free(s);
  86. return -EIO;
  87. }
  88. static int tcp_read(URLContext *h, UINT8 *buf, int size)
  89. {
  90. TCPContext *s = h->priv_data;
  91. int size1, len;
  92. size1 = size;
  93. while (size > 0) {
  94. len = read (s->fd, buf, size);
  95. if (len < 0) {
  96. if (errno != EINTR && errno != EAGAIN)
  97. return -errno;
  98. else
  99. continue;
  100. } else if (len == 0) {
  101. break;
  102. }
  103. size -= len;
  104. buf += len;
  105. }
  106. return size1 - size;
  107. }
  108. static int tcp_write(URLContext *h, UINT8 *buf, int size)
  109. {
  110. TCPContext *s = h->priv_data;
  111. int ret, size1;
  112. size1 = size;
  113. while (size > 0) {
  114. ret = write (s->fd, buf, size);
  115. if (ret < 0 && errno != EINTR && errno != EAGAIN)
  116. return -errno;
  117. size -= ret;
  118. buf += ret;
  119. }
  120. return size1 - size;
  121. }
  122. static int tcp_close(URLContext *h)
  123. {
  124. TCPContext *s = h->priv_data;
  125. close(s->fd);
  126. av_free(s);
  127. return 0;
  128. }
  129. URLProtocol tcp_protocol = {
  130. "tcp",
  131. tcp_open,
  132. tcp_read,
  133. tcp_write,
  134. NULL, /* seek */
  135. tcp_close,
  136. };