📄 ftp.c
字号:
} else { ftp_ReadCommand( p_access, NULL, NULL ); } net_Close( p_sys->fd_cmd ); /* free memory */ vlc_UrlClean( &p_sys->url ); free( p_sys );}/***************************************************************************** * Seek: try to go at the right place *****************************************************************************/static int Seek( access_t *p_access, int64_t i_pos ){ if( i_pos < 0 ) { return VLC_EGENERIC; } msg_Dbg( p_access, "seeking to "I64Fd, i_pos ); ftp_StopStream( p_access ); if( ftp_StartStream( p_access, i_pos ) < 0 ) { p_access->info.b_eof = VLC_TRUE; return VLC_EGENERIC; } p_access->info.b_eof = VLC_FALSE; p_access->info.i_pos = i_pos; return VLC_SUCCESS;}/***************************************************************************** * Read: *****************************************************************************/static int Read( access_t *p_access, uint8_t *p_buffer, int i_len ){ access_sys_t *p_sys = p_access->p_sys; int i_read; if( p_access->info.b_eof ) return 0; i_read = net_Read( p_access, p_sys->fd_data, NULL, p_buffer, i_len, VLC_FALSE ); if( i_read == 0 ) p_access->info.b_eof = VLC_TRUE; else if( i_read > 0 ) p_access->info.i_pos += i_read; return i_read;}/***************************************************************************** * Control: *****************************************************************************/static int Control( access_t *p_access, int i_query, va_list args ){ vlc_bool_t *pb_bool; int *pi_int; int64_t *pi_64; vlc_value_t val; switch( i_query ) { /* */ case ACCESS_CAN_SEEK: pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* ); *pb_bool = VLC_TRUE; break; case ACCESS_CAN_FASTSEEK: pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* ); *pb_bool = VLC_FALSE; break; case ACCESS_CAN_PAUSE: pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* ); *pb_bool = VLC_TRUE; /* FIXME */ break; case ACCESS_CAN_CONTROL_PACE: pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* ); *pb_bool = VLC_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" ) * I64C(1000); break; /* */ case ACCESS_SET_PAUSE_STATE: /* Nothing to do */ break; case ACCESS_GET_TITLE_INFO: case ACCESS_SET_TITLE: case ACCESS_SET_SEEKPOINT: case ACCESS_SET_PRIVATE_ID_STATE: return VLC_EGENERIC; default: msg_Warn( p_access, "unimplemented query in control" ); return VLC_EGENERIC; } return VLC_SUCCESS;}/***************************************************************************** * ftp_*: *****************************************************************************/static int ftp_SendCommand( access_t *p_access, char *psz_fmt, ... ){ access_sys_t *p_sys = p_access->p_sys; va_list args; char *psz_cmd; va_start( args, psz_fmt ); vasprintf( &psz_cmd, psz_fmt, args ); 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( access_t *p_access, int *pi_answer, char **ppsz_answer ){ access_sys_t *p_sys = p_access->p_sys; char *psz_line; int i_answer; psz_line = net_Gets( p_access, p_sys->fd_cmd, NULL ); msg_Dbg( p_access, "answer=%s", psz_line ); if( psz_line == NULL || strlen( psz_line ) < 3 ) { msg_Err( p_access, "cannot get answer" ); if( psz_line ) free( psz_line ); if( pi_answer ) *pi_answer = 500; if( ppsz_answer ) *ppsz_answer = NULL; return -1; } 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( access_t *p_access, off_t i_start ){ access_sys_t *p_sys = p_access->p_sys; char psz_ipv4[16], *psz_ip; int i_answer; char *psz_arg, *psz_parser; int i_port; psz_ip = p_sys->sz_epsv_ip; if( ( ftp_SendCommand( p_access, *psz_ip ? "EPSV" : "PASV" ) < 0 ) || ( ftp_ReadCommand( p_access, &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 != NULL ) { 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, "TYPE I" ) < 0 || ftp_ReadCommand( p_access, &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, "REST "I64Fu, i_start ) < 0 || ftp_ReadCommand( p_access, &i_answer, NULL ) > 3 ) { msg_Err( p_access, "cannot set restart point" ); 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, "RETR %s", p_sys->url.psz_path ) < 0 || ftp_ReadCommand( p_access, &i_answer, NULL ) > 2 ) { msg_Err( p_access, "cannot retreive file" ); return VLC_EGENERIC; } return VLC_SUCCESS;}static int ftp_StopStream ( access_t *p_access ){ access_sys_t *p_sys = p_access->p_sys; int i_answer; if( ftp_SendCommand( p_access, "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 > 0 ) { net_Close( p_sys->fd_data ); p_sys->fd_data = -1; ftp_ReadCommand( p_access, &i_answer, NULL ); } ftp_ReadCommand( p_access, &i_answer, NULL ); return VLC_SUCCESS;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -