?? ftpdwl0.c
字號:
#ifdef _CVI_
#include <utility.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ftpdwl.h"
#include "gprs_a.h"
int mystrncmpnocase ( char * p, char * q, int n );
#define FTP_RSP_LEN 128
static
unsigned
char szFTP[ FTP_RSP_LEN + 1 ];
int ftp_get_response ( int ctrl, unsigned char * presponse, int size, int timeout )
{
unsigned
char * p = presponse;
unsigned
char c[ 2 ];
int i;
while ( 1 )
{
i = 0;
while ( socket_read ( ctrl, c, 1 ) != 1 )
{
#ifdef _CVI_
if ( ++i >= 100 ) printf ( "\n\tSometing wrong?\n\n" );
#endif
if ( i >= 10000 ) return (*p = 0); // Here a timeout may be needed! Otherwise endless loop will cause // To Do...
}
if ( size-- > 0 ) *p++ = c[ 0 ];
if ( c[ 0 ] == '\n' ) break;
}
*p = '\0';
#ifdef _CVI_
printf ( "\nFTP RSP:\n%s\n\n", presponse );
#endif
return (int)(p - presponse);
}
int ftp_login ( int ctrl, unsigned char * pusername, unsigned char * ppassword )
{
ftp_get_response ( ctrl, szFTP, FTP_RSP_LEN, 0 );
if ( mystrncmpnocase ( (char *)szFTP, "220 ", 4 ) == 0 ) // Ready to login
{
sprintf ( (char *)szFTP, "USER %s\r\n", pusername );
socket_write ( ctrl, szFTP, strlen ( (char *)szFTP ) );
ftp_get_response ( ctrl, szFTP, FTP_RSP_LEN, 0 );
}
if ( mystrncmpnocase ( (char *)szFTP, "331 ", 4 ) == 0 ) // Password is needed
{
sprintf ( (char *)szFTP, "PASS %s\r\n", ppassword );
socket_write ( ctrl, szFTP, strlen ( (char *)szFTP ) );
ftp_get_response ( ctrl, szFTP, FTP_RSP_LEN, 0 );
}
if (!mystrncmpnocase ( (char *)szFTP, "230 ", 4 ) == 0 ) return 0; // fail to login
return 1;
}
int ftp_get_pasv_param ( unsigned char * presponse, unsigned char * pip, int * pport )
{
// 227 Entering Passive Mode (221,231,140,211,11,140)
int i = 0;
unsigned
char * p = presponse;
unsigned
char c;
while ( *p++ != '(' );
while ( i < 4 )
{
c = *p++;
if ( c == ',' )
{
*pip++ = '.';
i++;
}
else
*pip++ = c;
}
*(pip - 1 ) = '\0';
i = 0;
while ( (c=*p++) != ',' )
{
i = i * 10 + c - '0';
}
*pport = i << 8; //
i = 0;
while ( (c=*p++) != ')' )
{
i = i * 10 + c - '0';
}
*pport += i;
return 1;
}
int ftp_set_pasv ( int ctrl )
{
int i;
int data;
int port;
unsigned
char ip[ 20 ];
sprintf ( (char *)szFTP, "PASV\r\n" );
socket_write ( ctrl, szFTP, strlen ( (char *)szFTP ) );
ftp_get_response ( ctrl, szFTP, FTP_RSP_LEN, 0 );
if (!mystrncmpnocase ( (char *)szFTP, "227 ", 4 ) == 0 ) return 0; // fail to Entering Passive Mode
if (!ftp_get_pasv_param ( szFTP, ip, &port ) ) return 0;
for ( i = 5; i > 0; i-- )
{
data = socket_open ( SOCKET_TCP, ip, port );
if ( data > 0 ) break;
#ifdef _CVI_
printf ( "\n\n\tsocket(%d ) error!\n\n", data );
Delay ( 2 );
#endif
}
if ( i == 0 ) return 0;
return data;
}
int ftp_get_file ( int ctrl, int data, unsigned char * pfilename, int start, int size, CALLBACK_FTP fn )
{
int nBytes = 0;
int nbytes = 0;
// if ( ctrl <= 0 ) return 0;
// if ( data <= 0 ) return 0;
// if (!pfilename ) return 0;
// set the indicator of the first byte to retrieve from file
sprintf ( (char *)szFTP, "REST %d\r\n", start );
socket_write ( ctrl, szFTP, strlen ( (char *)szFTP ) );
ftp_get_response ( ctrl, szFTP, FTP_RSP_LEN, 0 );
if (!mystrncmpnocase ( (char *)szFTP, "350 ", 4 ) == 0 ) return 0; // fail to Reset to the byte at 'start'
// binary file to be transfered
sprintf ( (char *)szFTP, "TYPE I\r\n" );
socket_write ( ctrl, szFTP, strlen ( (char *)szFTP ) );
ftp_get_response ( ctrl, szFTP, FTP_RSP_LEN, 0 );
if (!mystrncmpnocase ( (char *)szFTP, "200 ", 4 ) == 0 ) return 0; // fail to Entering BINARY Mode
// start to transfer file
sprintf ( (char *)szFTP, "RETR %s\r\n", pfilename );
socket_write ( ctrl, szFTP, strlen ( (char *)szFTP ) );
// ftp_get_response ( ctrl, szFTP, FTP_RSP_LEN, 0 ); // mark it to avoid buffer of data stream overrun!
// if (!mystrncmpnocase ( szFTP, "150 ", 4 ) == 0 ) return 0; // there is a error!
while ( 1 )
{
socket_read ( ctrl, szFTP, FTP_RSP_LEN );
if ( FTP_RSP_LEN < size ) nbytes = FTP_RSP_LEN;
else nbytes = size;
nbytes = socket_read ( data, szFTP, nbytes );
if ( nbytes <= 0 )
{
#ifdef _CVI_
static int c = 0;
if ( ++c >= 500 )
{
c = 0;
printf ( "\n\n\tI'm super hungry!\n\n" );
}
#endif
continue; // Here a timeout may be needed! Otherwise endless loop will cause // To Do...
}
// logfile ( "USERTEST", szFTP, nbytes );
if ( fn ) fn ( szFTP, nbytes, start + nBytes );
nBytes += nbytes;
size -= nbytes;
#ifdef _CVI_
printf ( "\n\n\tByte recieved : %08d\n\n", nBytes );
#endif
if ( size <= 0 ) break;
}
return nBytes;
}
int ftp_dwl ( unsigned char * pip, int port, unsigned char * pusername, unsigned char * ppassword, unsigned char * pfilename, int start, int size, CALLBACK_FTP fn )
{
int nBytes = 0;
int ctrl;
int data;
int i;
for ( i = 5; i > 0; i-- )
{
ctrl = socket_open ( SOCKET_TCP, pip, port );
if ( ctrl > 0 ) break;
#ifdef _CVI_
printf ( "\n\n\tsocket(%d ) error!\n\n", ctrl );
Delay ( 2 );
#endif
}
if ( i == 0 ) return 0;
if ( ftp_login ( ctrl, pusername, ppassword ) )
{
data = ftp_set_pasv ( ctrl );
if ( data > 0 )
{
nBytes = ftp_get_file ( ctrl, data, pfilename, start, size, fn );
socket_close ( data );
}
}
socket_close ( ctrl );
return nBytes;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -