⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ftp.c

📁 vlc源码
💻 C
📖 第 1 页 / 共 2 页
字号:
}/***************************************************************************** * Close: free unused data structures *****************************************************************************/static void Close( vlc_object_t *p_access, access_sys_t *p_sys ){    msg_Dbg( p_access, "stopping stream" );    ftp_StopStream( p_access, p_sys );    if( ftp_SendCommand( p_access, p_sys, "QUIT" ) < 0 )    {        msg_Warn( p_access, "cannot quit" );    }    else    {        ftp_ReadCommand( p_access, p_sys, NULL, NULL );    }    net_Close( p_sys->fd_cmd );    /* free memory */    vlc_UrlClean( &p_sys->url );    free( p_sys );}static void InClose( vlc_object_t *p_this ){    Close( p_this, ((access_t *)p_this)->p_sys);}static void OutClose( vlc_object_t *p_this ){    Close( p_this, GET_OUT_SYS(p_this));}/***************************************************************************** * Seek: try to go at the right place *****************************************************************************/static int _Seek( vlc_object_t *p_access, access_sys_t *p_sys, int64_t i_pos ){    if( i_pos < 0 )        return VLC_EGENERIC;    msg_Dbg( p_access, "seeking to %"PRId64, i_pos );    ftp_StopStream( (vlc_object_t *)p_access, p_sys );    if( ftp_StartStream( (vlc_object_t *)p_access, p_sys, i_pos ) < 0 )        return VLC_EGENERIC;    return VLC_SUCCESS;}static int Seek( access_t *p_access, int64_t i_pos ){    int val = _Seek( (vlc_object_t *)p_access, p_access->p_sys, i_pos );    if( val )        return val;    p_access->info.b_eof = false;    p_access->info.i_pos = i_pos;    return VLC_SUCCESS;}static int OutSeek( sout_access_out_t *p_access, off_t i_pos ){    return _Seek( (vlc_object_t *)p_access, GET_OUT_SYS( p_access ), i_pos);}/***************************************************************************** * Read: *****************************************************************************/static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len ){    access_sys_t *p_sys = p_access->p_sys;    int i_read;    assert( p_sys->fd_data != -1 );    assert( !p_sys->out );    if( p_access->info.b_eof )        return 0;    i_read = net_Read( p_access, p_sys->fd_data, NULL, p_buffer, i_len,                       false );    if( i_read == 0 )        p_access->info.b_eof = true;    else if( i_read > 0 )        p_access->info.i_pos += i_read;    return i_read;}/***************************************************************************** * Write: *****************************************************************************/static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer ){    access_sys_t *p_sys = GET_OUT_SYS(p_access);    size_t i_write = 0;    assert( p_sys->fd_data != -1 );    while( p_buffer != NULL )    {        block_t *p_next = p_buffer->p_next;;        i_write += net_Write( p_access, p_sys->fd_data, NULL,                              p_buffer->p_buffer, p_buffer->i_buffer );        block_Release( p_buffer );        p_buffer = p_next;    }    return i_write;}/***************************************************************************** * Control: *****************************************************************************/static int Control( access_t *p_access, int i_query, va_list args ){    bool   *pb_bool;    int          *pi_int;    int64_t      *pi_64;    vlc_value_t  val;    switch( i_query )    {        /* */        case ACCESS_CAN_SEEK:            pb_bool = (bool*)va_arg( args, bool* );            *pb_bool = true;            break;        case ACCESS_CAN_FASTSEEK:            pb_bool = (bool*)va_arg( args, bool* );            *pb_bool = false;            break;        case ACCESS_CAN_PAUSE:            pb_bool = (bool*)va_arg( args, bool* );            *pb_bool = true;    /* FIXME */            break;        case ACCESS_CAN_CONTROL_PACE:            pb_bool = (bool*)va_arg( args, bool* );            *pb_bool = true;    /* FIXME */            break;        /* */        case ACCESS_GET_MTU:            pi_int = (int*)va_arg( args, int * );            *pi_int = 0;            break;        case ACCESS_GET_PTS_DELAY:            pi_64 = (int64_t*)va_arg( args, int64_t * );            var_Get( p_access, "ftp-caching", &val );            *pi_64 = (int64_t)var_GetInteger( p_access, "ftp-caching" ) * INT64_C(1000);            break;        /* */        case ACCESS_SET_PAUSE_STATE:            pb_bool = (bool*)va_arg( args, bool* );            if ( !pb_bool )              return Seek( p_access, p_access->info.i_pos );            break;        case ACCESS_GET_TITLE_INFO:        case ACCESS_SET_TITLE:        case ACCESS_SET_SEEKPOINT:        case ACCESS_SET_PRIVATE_ID_STATE:        case ACCESS_GET_CONTENT_TYPE:        case ACCESS_GET_META:            return VLC_EGENERIC;        default:            msg_Warn( p_access, "unimplemented query in control: %d", i_query);            return VLC_EGENERIC;    }    return VLC_SUCCESS;}/***************************************************************************** * ftp_*: *****************************************************************************/static int ftp_SendCommand( vlc_object_t *p_access, access_sys_t *p_sys,                            const char *psz_fmt, ... ){    va_list      args;    char         *psz_cmd;    va_start( args, psz_fmt );    if( vasprintf( &psz_cmd, psz_fmt, args ) == -1 )        return VLC_EGENERIC;    va_end( args );    msg_Dbg( p_access, "ftp_SendCommand:\"%s\"", psz_cmd);    if( net_Printf( VLC_OBJECT(p_access), p_sys->fd_cmd, NULL, "%s\r\n",                    psz_cmd ) < 0 )    {        msg_Err( p_access, "failed to send command" );        return VLC_EGENERIC;    }    return VLC_SUCCESS;}/* TODO support this s**t : RFC 959 allows the client to send certain TELNET strings at any moment, even in the middle of a request: * \377\377. * \377\376x where x is one byte. * \377\375x where x is one byte. The server is obliged to send \377\374x *                                immediately after reading x. * \377\374x where x is one byte. * \377\373x where x is one byte. The server is obliged to send \377\376x *                                immediately after reading x. * \377x for any other byte x. These strings are not part of the requests, except in the case \377\377, where the request contains one \377. */static int ftp_ReadCommand( vlc_object_t *p_access, access_sys_t *p_sys,                            int *pi_answer, char **ppsz_answer ){    char         *psz_line;    int          i_answer;    psz_line = net_Gets( p_access, p_sys->fd_cmd, NULL );    if( psz_line == NULL || strlen( psz_line ) < 3 )    {        msg_Err( p_access, "cannot get answer" );        free( psz_line );        if( pi_answer ) *pi_answer    = 500;        if( ppsz_answer ) *ppsz_answer  = NULL;        return -1;    }    msg_Dbg( p_access, "answer=%s", psz_line );    if( psz_line[3] == '-' )    /* Multiple response */    {        char end[4];        memcpy( end, psz_line, 3 );        end[3] = ' ';        for( ;; )        {            char *psz_tmp = net_Gets( p_access, p_sys->fd_cmd, NULL );            if( psz_tmp == NULL )   /* Error */                break;            if( !strncmp( psz_tmp, end, 4 ) )            {                free( psz_tmp );                break;            }            free( psz_tmp );        }    }    i_answer = atoi( psz_line );    if( pi_answer ) *pi_answer = i_answer;    if( ppsz_answer )    {        *ppsz_answer = psz_line;    }    else    {        free( psz_line );    }    return( i_answer / 100 );}static int ftp_StartStream( vlc_object_t *p_access, access_sys_t *p_sys,                            int64_t i_start ){    char psz_ipv4[16], *psz_ip = p_sys->sz_epsv_ip;    int  i_answer;    char *psz_arg, *psz_parser;    int  i_port;    assert( p_sys->fd_data == -1 );    if( ( ftp_SendCommand( p_access, p_sys, *psz_ip ? "EPSV" : "PASV" ) < 0 )     || ( ftp_ReadCommand( p_access, p_sys, &i_answer, &psz_arg ) != 2 ) )    {        msg_Err( p_access, "cannot set passive mode" );        return VLC_EGENERIC;    }    psz_parser = strchr( psz_arg, '(' );    if( psz_parser == NULL )    {        free( psz_arg );        msg_Err( p_access, "cannot parse passive mode response" );        return VLC_EGENERIC;    }    if( *psz_ip )    {        char psz_fmt[7] = "(|||%u";        psz_fmt[1] = psz_fmt[2] = psz_fmt[3] = psz_parser[1];        if( sscanf( psz_parser, psz_fmt, &i_port ) < 1 )        {            free( psz_arg );            msg_Err( p_access, "cannot parse passive mode response" );            return VLC_EGENERIC;        }    }    else    {        unsigned a1, a2, a3, a4, p1, p2;        if( ( sscanf( psz_parser, "(%u,%u,%u,%u,%u,%u", &a1, &a2, &a3, &a4,                      &p1, &p2 ) < 6 ) || ( a1 > 255 ) || ( a2 > 255 )         || ( a3 > 255 ) || ( a4 > 255 ) || ( p1 > 255 ) || ( p2 > 255 ) )        {            free( psz_arg );            msg_Err( p_access, "cannot parse passive mode response" );            return VLC_EGENERIC;        }        sprintf( psz_ipv4, "%u.%u.%u.%u", a1, a2, a3, a4 );        psz_ip = psz_ipv4;        i_port = (p1 << 8) | p2;    }    free( psz_arg );    msg_Dbg( p_access, "ip:%s port:%d", psz_ip, i_port );    if( ftp_SendCommand( p_access, p_sys, "TYPE I" ) < 0 ||        ftp_ReadCommand( p_access, p_sys, &i_answer, NULL ) != 2 )    {        msg_Err( p_access, "cannot set binary transfer mode" );        return VLC_EGENERIC;    }    if( i_start > 0 )    {        if( ftp_SendCommand( p_access, p_sys, "REST %"PRIu64, i_start ) < 0 ||            ftp_ReadCommand( p_access, p_sys, &i_answer, NULL ) > 3 )        {            msg_Err( p_access, "cannot set restart offset" );            return VLC_EGENERIC;        }    }    msg_Dbg( p_access, "waiting for data connection..." );    p_sys->fd_data = net_ConnectTCP( p_access, psz_ip, i_port );    if( p_sys->fd_data < 0 )    {        msg_Err( p_access, "failed to connect with server" );        return VLC_EGENERIC;    }    msg_Dbg( p_access, "connection with \"%s:%d\" successful",             psz_ip, i_port );    /* "1xx" message */    if( ftp_SendCommand( p_access, p_sys, "%s %s",                         p_sys->out ? "STOR" : "RETR",                         p_sys->url.psz_path ?: "" ) < 0 ||        ftp_ReadCommand( p_access, p_sys, &i_answer, NULL ) > 2 )    {        msg_Err( p_access, "cannot retrieve file" );        return VLC_EGENERIC;    }    shutdown( p_sys->fd_data, p_sys->out ? SHUT_RD : SHUT_WR );    return VLC_SUCCESS;}static int ftp_StopStream ( vlc_object_t *p_access, access_sys_t *p_sys ){    if( ftp_SendCommand( p_access, p_sys, "ABOR" ) < 0 )    {        msg_Warn( p_access, "cannot abort file" );        if(  p_sys->fd_data > 0 )            net_Close( p_sys->fd_data );        p_sys->fd_data = -1;        return VLC_EGENERIC;    }    if( p_sys->fd_data != -1 )    {        net_Close( p_sys->fd_data );        p_sys->fd_data = -1;        /* Read the final response from RETR/STOR, i.e. 426 or 226 */        ftp_ReadCommand( p_access, p_sys, NULL, NULL );    }    /* Read the response from ABOR, i.e. 226 or 225 */    ftp_ReadCommand( p_access, p_sys, NULL, NULL );    return VLC_SUCCESS;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -