123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- /*
- * TCP protocol
- * Copyright (c) 2002 Fabrice Bellard
- *
- * This file is part of Libav.
- *
- * Libav is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * Libav is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Libav; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
- #include "avformat.h"
- #include <unistd.h>
- #include "internal.h"
- #include "network.h"
- #include "os_support.h"
- #if HAVE_POLL_H
- #include <poll.h>
- #endif
- #include <sys/time.h>
- typedef struct TCPContext {
- int fd;
- } TCPContext;
- /* return non zero if error */
- static int tcp_open(URLContext *h, const char *uri, int flags)
- {
- struct addrinfo hints, *ai, *cur_ai;
- int port, fd = -1;
- TCPContext *s = NULL;
- int ret;
- socklen_t optlen;
- char hostname[1024],proto[1024],path[1024];
- char portstr[10];
- av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
- &port, path, sizeof(path), uri);
- if (strcmp(proto,"tcp") || port <= 0 || port >= 65536)
- return AVERROR(EINVAL);
- memset(&hints, 0, sizeof(hints));
- hints.ai_family = AF_UNSPEC;
- hints.ai_socktype = SOCK_STREAM;
- snprintf(portstr, sizeof(portstr), "%d", port);
- ret = getaddrinfo(hostname, portstr, &hints, &ai);
- if (ret) {
- av_log(NULL, AV_LOG_ERROR,
- "Failed to resolve hostname %s: %s\n",
- hostname, gai_strerror(ret));
- return AVERROR(EIO);
- }
- cur_ai = ai;
- restart:
- fd = socket(cur_ai->ai_family, cur_ai->ai_socktype, cur_ai->ai_protocol);
- if (fd < 0)
- goto fail;
- ff_socket_nonblock(fd, 1);
- redo:
- ret = connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
- if (ret < 0) {
- struct pollfd p = {fd, POLLOUT, 0};
- if (ff_neterrno() == AVERROR(EINTR)) {
- if (url_interrupt_cb()) {
- ret = AVERROR_EXIT;
- goto fail1;
- }
- goto redo;
- }
- if (ff_neterrno() != AVERROR(EINPROGRESS) &&
- ff_neterrno() != AVERROR(EAGAIN))
- goto fail;
- /* wait until we are connected or until abort */
- for(;;) {
- if (url_interrupt_cb()) {
- ret = AVERROR_EXIT;
- goto fail1;
- }
- ret = poll(&p, 1, 100);
- if (ret > 0)
- break;
- }
- /* test error */
- optlen = sizeof(ret);
- getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen);
- if (ret != 0) {
- av_log(NULL, AV_LOG_ERROR,
- "TCP connection to %s:%d failed: %s\n",
- hostname, port, strerror(ret));
- goto fail;
- }
- }
- s = av_malloc(sizeof(TCPContext));
- if (!s) {
- freeaddrinfo(ai);
- return AVERROR(ENOMEM);
- }
- h->priv_data = s;
- h->is_streamed = 1;
- s->fd = fd;
- freeaddrinfo(ai);
- return 0;
- fail:
- if (cur_ai->ai_next) {
- /* Retry with the next sockaddr */
- cur_ai = cur_ai->ai_next;
- if (fd >= 0)
- closesocket(fd);
- goto restart;
- }
- ret = AVERROR(EIO);
- fail1:
- if (fd >= 0)
- closesocket(fd);
- freeaddrinfo(ai);
- return ret;
- }
- static int tcp_wait_fd(int fd, int write)
- {
- int ev = write ? POLLOUT : POLLIN;
- struct pollfd p = { .fd = fd, .events = ev, .revents = 0 };
- int ret;
- ret = poll(&p, 1, 100);
- return ret < 0 ? ff_neterrno() : p.revents & ev ? 0 : AVERROR(EAGAIN);
- }
- static int tcp_read(URLContext *h, uint8_t *buf, int size)
- {
- TCPContext *s = h->priv_data;
- int ret;
- if (!(h->flags & URL_FLAG_NONBLOCK)) {
- ret = tcp_wait_fd(s->fd, 0);
- if (ret < 0)
- return ret;
- }
- ret = recv(s->fd, buf, size, 0);
- return ret < 0 ? ff_neterrno() : ret;
- }
- static int tcp_write(URLContext *h, const uint8_t *buf, int size)
- {
- TCPContext *s = h->priv_data;
- int ret;
- if (!(h->flags & URL_FLAG_NONBLOCK)) {
- ret = tcp_wait_fd(s->fd, 1);
- if (ret < 0)
- return ret;
- }
- ret = send(s->fd, buf, size, 0);
- return ret < 0 ? ff_neterrno() : ret;
- }
- static int tcp_close(URLContext *h)
- {
- TCPContext *s = h->priv_data;
- closesocket(s->fd);
- av_free(s);
- return 0;
- }
- static int tcp_get_file_handle(URLContext *h)
- {
- TCPContext *s = h->priv_data;
- return s->fd;
- }
- URLProtocol ff_tcp_protocol = {
- "tcp",
- tcp_open,
- tcp_read,
- tcp_write,
- NULL, /* seek */
- tcp_close,
- .url_get_file_handle = tcp_get_file_handle,
- };
|