Skip to content
Snippets Groups Projects
tcp.c 5.12 KiB
Newer Older
  • Learn to ignore specific revisions
  • Fabrice Bellard's avatar
    Fabrice Bellard committed
    /*
     * TCP protocol
    
     * Copyright (c) 2002 Fabrice Bellard
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
     *
    
     * This file is part of Libav.
    
     * Libav is free software; you can redistribute it and/or
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
     * 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.
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
     *
    
     * Libav is distributed in the hope that it will be useful,
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
     * 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
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
     */
    #include "avformat.h"
    
    #include "libavutil/parseutils.h"
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    #include <unistd.h>
    
    #include "network.h"
    
    #include "os_support.h"
    
    #if HAVE_POLL_H
    #include <poll.h>
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    #include <sys/time.h>
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
    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;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        int port, fd = -1;
    
        int listen_socket = 0;
        const char *p;
        char buf[256];
    
        int ret;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        socklen_t optlen;
    
        char hostname[1024],proto[1024],path[1024];
    
        av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
    
            &port, path, sizeof(path), uri);
        if (strcmp(proto,"tcp") || port <= 0 || port >= 65536)
    
        p = strchr(uri, '?');
        if (p) {
            if (av_find_info_tag(buf, sizeof(buf), "listen", p))
                listen_socket = 1;
        }
    
        memset(&hints, 0, sizeof(hints));
    
        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));
    
        cur_ai = ai;
    
     restart:
        fd = socket(cur_ai->ai_family, cur_ai->ai_socktype, cur_ai->ai_protocol);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        if (fd < 0)
    
        if (listen_socket) {
            int fd1;
            ret = bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
            listen(fd, 1);
            fd1 = accept(fd, NULL, NULL);
            closesocket(fd);
            fd = fd1;
        } else {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
     redo:
    
            ret = connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
        }
    
        ff_socket_nonblock(fd, 1);
    
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        if (ret < 0) {
    
            struct pollfd p = {fd, POLLOUT, 0};
    
            if (ff_neterrno() == AVERROR(EINTR)) {
    
                if (url_interrupt_cb()) {
                    ret = AVERROR_EXIT;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                goto redo;
    
            if (ff_neterrno() != AVERROR(EINPROGRESS) &&
                ff_neterrno() != AVERROR(EAGAIN))
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                goto fail;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            /* wait until we are connected or until abort */
            for(;;) {
                if (url_interrupt_cb()) {
    
                    ret = AVERROR_EXIT;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                    goto fail1;
                }
    
                ret = poll(&p, 1, 100);
                if (ret > 0)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                    break;
            }
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            /* 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));
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                goto fail;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        s->fd = fd;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        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;
        }
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
     fail1:
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        if (fd >= 0)
    
            closesocket(fd);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        return ret;
    
    static int tcp_read(URLContext *h, uint8_t *buf, int size)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
        TCPContext *s = h->priv_data;
    
        int ret;
    
        if (!(h->flags & URL_FLAG_NONBLOCK)) {
    
            ret = ff_network_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)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
        TCPContext *s = h->priv_data;
    
        int ret;
    
        if (!(h->flags & URL_FLAG_NONBLOCK)) {
    
            ret = ff_network_wait_fd(s->fd, 1);
    
            if (ret < 0)
    
                return ret;
    
        ret = send(s->fd, buf, size, 0);
        return ret < 0 ? ff_neterrno() : ret;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    }
    
    static int tcp_close(URLContext *h)
    {
        TCPContext *s = h->priv_data;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        av_free(s);
        return 0;
    }
    
    
    static int tcp_get_file_handle(URLContext *h)
    {
        TCPContext *s = h->priv_data;
        return s->fd;
    }
    
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        "tcp",
        tcp_open,
        tcp_read,
        tcp_write,
        NULL, /* seek */
        tcp_close,
    
        .url_get_file_handle = tcp_get_file_handle,
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    };