Skip to content
Snippets Groups Projects
tcp.c 5.68 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 FFmpeg.
     *
     * FFmpeg 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
     *
    
     * FFmpeg 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 FFmpeg; 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 <unistd.h>
    
    #include "network.h"
    
    #include "os_support.h"
    
    #if HAVE_SYS_SELECT_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;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        fd_set wfds;
        int fd_max, ret;
        struct timeval tv;
        socklen_t optlen;
    
        char hostname[1024],proto[1024],path[1024];
    
        ff_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
    
            &port, path, sizeof(path), uri);
        if (strcmp(proto,"tcp") || port <= 0 || port >= 65536)
    
        memset(&hints, 0, sizeof(hints));
    
        hints.ai_socktype = SOCK_STREAM;
        snprintf(portstr, sizeof(portstr), "%d", port);
        if (getaddrinfo(hostname, portstr, &hints, &ai))
    
        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)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
     redo:
    
        ret = connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        if (ret < 0) {
    
            if (ff_neterrno() == FF_NETERROR(EINTR))
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                goto redo;
    
            if (ff_neterrno() != FF_NETERROR(EINPROGRESS) &&
                ff_neterrno() != FF_NETERROR(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()) {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                    goto fail1;
                }
                fd_max = fd;
                FD_ZERO(&wfds);
                FD_SET(fd, &wfds);
                tv.tv_sec = 0;
                tv.tv_usec = 100 * 1000;
                ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);
                if (ret > 0 && FD_ISSET(fd, &wfds))
                    break;
            }
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            /* test error */
            optlen = sizeof(ret);
            getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen);
            if (ret != 0)
                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;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        fd_set rfds;
        struct timeval tv;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            if (url_interrupt_cb())
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            fd_max = s->fd;
            FD_ZERO(&rfds);
            FD_SET(s->fd, &rfds);
            tv.tv_sec = 0;
            tv.tv_usec = 100 * 1000;
    
            ret = select(fd_max + 1, &rfds, NULL, NULL, &tv);
            if (ret > 0 && FD_ISSET(s->fd, &rfds)) {
                len = recv(s->fd, buf, size, 0);
                if (len < 0) {
    
                    if (ff_neterrno() != FF_NETERROR(EINTR) &&
                        ff_neterrno() != FF_NETERROR(EAGAIN))
    
                        return AVERROR(ff_neterrno());
    
                if (ff_neterrno() == FF_NETERROR(EINTR))
                    continue;
    
    static int tcp_write(URLContext *h, uint8_t *buf, int size)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
        TCPContext *s = h->priv_data;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        fd_set wfds;
        struct timeval tv;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
        size1 = size;
        while (size > 0) {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            if (url_interrupt_cb())
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            fd_max = s->fd;
            FD_ZERO(&wfds);
            FD_SET(s->fd, &wfds);
            tv.tv_sec = 0;
            tv.tv_usec = 100 * 1000;
    
            ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);
            if (ret > 0 && FD_ISSET(s->fd, &wfds)) {
                len = send(s->fd, buf, size, 0);
                if (len < 0) {
    
                    if (ff_neterrno() != FF_NETERROR(EINTR) &&
                        ff_neterrno() != FF_NETERROR(EAGAIN))
    
                        return AVERROR(ff_neterrno());
    
                if (ff_neterrno() == FF_NETERROR(EINTR))
                    continue;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        }
        return size1 - size;
    }
    
    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
    URLProtocol tcp_protocol = {
        "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
    };