📄 ftpdwl.c
字号:
#ifdef _CVI_
#include <userint.h>
#include <utility.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ftpdwl.h"
#include "gprs_a.h"
#include "crc16.h"
#include "config.h"
int mystrncmpnocase ( char * p, char * q, int n );
int ftp_get_response ( t_ftp * pftp )
{
unsigned
char * p = pftp->pbuf;
int size = pftp->nbufsize;
int tick;
while ( 1 )
{
tick = 0;
while ( socket_read ( pftp->ctrl, p, 1 ) != 1 )
{
#ifdef _CVI_
Delay ( 1 );
#else
OSTimeDlyHMSM(0,0,1,0);
#endif
#ifdef _CVI_
if ( ++tick >= 0x20 ) return (*p = 0); // To Do...
#else
// if ( ++tick >= pftp->timeout ) return (*p = 0); // To Do...
if ( ++tick >= 0x20 ) return (*p = 0); // To Do...
#endif // Here a timeout may be needed! Otherwise endless loop will cause
#ifdef _CVI_
if ( (tick & 0xF) == 0 )
printf ( "\n\tSomething wrong??\n\n" );
#else
if ( (tick & 0xF) == 0 )
if ( pftp->fn ) pftp->fn ( (unsigned char *)"\r\n\tSomething wrong??\r\n\r\n", strlen ( "\r\n\tSomething wrong??\r\n\r\n" ), -1 );
#endif
}
if ( size-- > 0 )
{
if ( *p++ == '\n' ) break;
}
else
{
if ( *p == '\n' ) break;
}
}
*p = '\0';
#ifdef _CVI_
printf ( "\nFTP RSP:\n%s\n\n", pftp->pbuf );
#endif
return (int)(p - pftp->pbuf);
}
int ftp_login ( t_ftp * pftp )
{
do {
ftp_get_response ( pftp );
} while ( mystrncmpnocase ( (char *)pftp->pbuf, "220-", 4 ) == 0 );
if ( mystrncmpnocase ( (char *)pftp->pbuf, "220 ", 4 ) == 0 ) // Ready to login
{
if ( pftp->fn ) pftp->fn ( (unsigned char *)"\r\n", 2, -1 );
sprintf ( (char *)pftp->pbuf, "USER %s\r\n", pftp->pusername );
if ( pftp->fn ) pftp->fn ( pftp->pbuf, strlen ( (char *)pftp->pbuf ), -1 );
socket_write ( pftp->ctrl, pftp->pbuf, strlen ( (char *)pftp->pbuf ) );
ftp_get_response ( pftp );
}
if ( mystrncmpnocase ( (char *)pftp->pbuf, "331 ", 4 ) == 0 ) // Password is needed
{
if ( pftp->fn ) pftp->fn ( (unsigned char *)"\r\n", 2, -1 );
sprintf ( (char *)pftp->pbuf, "PASS %s\r\n", pftp->ppassword );
if ( pftp->fn ) pftp->fn ( pftp->pbuf, strlen ( (char *)pftp->pbuf ), -1 );
socket_write ( pftp->ctrl, pftp->pbuf, strlen ( (char *)pftp->pbuf ) );
ftp_get_response ( pftp );
}
if ( mystrncmpnocase ( (char *)pftp->pbuf, "230 ", 4 ) != 0 ) return 0; // fail to login
return 1;
}
int ftp_get_pasv_param ( t_ftp * pftp )
{
// 227 Entering Passive Mode (221,231,140,211,11,140)
int i = 0;
unsigned
char * p = pftp->pbuf;
unsigned
char c;
while ( *p++ != '(' ); // BUG!!! if '(' is missed or meet NULL
pftp->pip2 = p;
while ( i < 4 ) // BUG!!! if ',' is missed or meet NULL
{
if ( *p == ',' )
{
*p = '.';
i++;
}
p++;
}
*(p - 1) = '\0';
i = 0;
while ( (c=*p++) != ',' ) // BUG!!! if ',' is missed or meet NULL
{
i = i * 10 + c - '0';
}
pftp->port2 = i << 8;
i = 0;
while ( (c=*p++) != ')' ) // BUG!!! if ')' is missed or meet NULL
{
i = i * 10 + c - '0';
}
pftp->port2 += i;
return 1;
}
int ftp_set_pasv ( t_ftp * pftp )
{
int i;
if ( pftp->fn ) pftp->fn ( (unsigned char *)"\r\n", 2, -1 );
sprintf ( (char *)pftp->pbuf, "PASV\r\n" );
if ( pftp->fn ) pftp->fn ( pftp->pbuf, strlen ( (char *)pftp->pbuf ), -1 );
socket_write ( pftp->ctrl, pftp->pbuf, strlen ( (char *)pftp->pbuf ) );
ftp_get_response ( pftp );
if ( mystrncmpnocase ( (char *)pftp->pbuf, "227 ", 4 ) != 0 ) return 0; // fail to Entering Passive Mode
if (!ftp_get_pasv_param ( pftp ) ) return 0;
for ( i = 3; i > 0; i-- )
{
pftp->data = socket_open ( 2, SOCKET_TCP, pftp->pip2, pftp->port2 );
if ( pftp->data > 0 ) return pftp->data;
#ifdef _CVI_
printf ( "\n\n\tsocket(%d ) error!\n\n", pftp->data );
Delay ( 2 );
#endif
}
return 0;
}
int ftp_set_param ( t_ftp * pftp )
{
// binary file to be transfered
if ( pftp->fn ) pftp->fn ( (unsigned char *)"\r\n", 2, -1 );
sprintf ( (char *)pftp->pbuf, "TYPE I\r\n" );
if ( pftp->fn ) pftp->fn ( pftp->pbuf, strlen ( (char *)pftp->pbuf ), -1 );
socket_write ( pftp->ctrl, pftp->pbuf, strlen ( (char *)pftp->pbuf ) );
ftp_get_response ( pftp );
if ( mystrncmpnocase ( (char *)pftp->pbuf, "200 ", 4 ) != 0 ) return 0; // fail to Entering BINARY Mode
// set the indicator of the first byte to retrieve from file
if ( pftp->fn ) pftp->fn ( (unsigned char *)"\r\n", 2, -1 );
sprintf ( (char *)pftp->pbuf, "REST %d\r\n", pftp->nbytes );
if ( pftp->fn ) pftp->fn ( pftp->pbuf, strlen ( (char *)pftp->pbuf ), -1 );
socket_write ( pftp->ctrl, pftp->pbuf, strlen ( (char *)pftp->pbuf ) );
ftp_get_response ( pftp );
if ( mystrncmpnocase ( (char *)pftp->pbuf, "350 ", 4 ) != 0 ) return 0; // fail to Reset to the byte at 'start'
return 1;
}
int tick = 0;
int nbytes = 0;
int nBytes = 0;
int ftp_dwl ( t_ftp * pftp )
{
int errors = 0;
int offset;
int n;
pftp->port2 = 0;
pftp->pip2 = 0;
pftp->ctrl = 0;
pftp->data = 0;
offset = pftp->nbytes;
// nBytes = pftp->nbytes;
nBytes = pftp->nfilesize;
nbytes = 0;
while ( pftp->nbytes < pftp->nfilesize ) // when bytes received still less than file size
{
// n = PACKSIZE + CRC_SIZE - nbytes
if ( nBytes <= pftp->nbytes ) nBytes += 10240; // set receiving boundary in this loop
if ( nBytes > pftp->nfilesize ) nBytes = pftp->nfilesize;
#ifdef _TW_
// if ( nbytes++ >= 5 ) GPRS_restart ( (unsigned char *)"INTERNET" ); // just as a counter at this moment // To Do... // GSM_restart ();
#else
// if ( nbytes++ >= 5 ) GPRS_restart ( (unsigned char *)"CMNET" ); // just as a counter at this moment // To Do... // GSM_restart ();
#endif
if ( errors++ == 5 )
{
GPRS_disconnect ();
// GPRS_reset (); // just as a counter at this moment // To Do... // GSM_restart ();
#ifdef _CVI_
Delay ( 3 );
#else
OSTimeDlyHMSM(0,0,3,0);
#endif
}
if ( errors >= 10 )
{
// GPRS_disconnect ();
GPRS_reset (); // just as a counter at this moment // To Do... // GSM_restart ();
errors = 0;
#ifdef _CVI_
Delay ( 3 );
#else
OSTimeDlyHMSM(0,0,3,0);
#endif
}
if ( pftp->fn ) pftp->fn ( (unsigned char *)"open ctrl\r\n", strlen ( "open ctrl\r\n" ), -1 );
pftp->ctrl = socket_open ( 1, SOCKET_TCP, pftp->pip, pftp->port );
if ( pftp->ctrl <= 0 ) continue;
if ( pftp->fn ) pftp->fn ( (unsigned char *)"ctrl opened\r\n", strlen ( "ctrl opened\r\n" ), -1 );
nbytes = 0;
if ( ftp_login ( pftp ) > 0 )
{
if ( ftp_set_param ( pftp ) > 0 )
{
if ( ftp_set_pasv ( pftp ) > 0 )
{
// start to transfer file
if ( pftp->fn ) pftp->fn ( (unsigned char *)"\r\n", 2, -1 );
sprintf ( (char *)pftp->pbuf, "RETR %s\r\n", pftp->pfilename );
if ( pftp->fn ) pftp->fn ( pftp->pbuf, strlen ( (char *)pftp->pbuf ), -1 );
#ifdef _CVI_
Delay ( 1 );
#else
OSTimeDlyHMSM(0,0,1,0);
#endif
socket_write ( pftp->ctrl, pftp->pbuf, strlen ( (char *)pftp->pbuf ) );
// ftp_get_response ( pftp ); // mark it to avoid buffer of data stream overrun!
// if ( mystrncmpnocase ( (char *)pftp->pbuf, "150 ", 4 ) == 0 );
while ( pftp->nbytes + nbytes < nBytes ) // while not reach the boundary
{
n = nBytes - pftp->nbytes;
if ( n > pftp->nbufsize - nbytes ) n = pftp->nbufsize - nbytes;
n = socket_read ( pftp->data, pftp->pbuf + nbytes, n );
if ( n <= 0 )
{
if ( socket_read ( pftp->ctrl, pftp->pbuf, pftp->nbufsize ) > 0 ) // drop responses
if ( pftp->fn ) pftp->fn ( pftp->pbuf, strlen ( (char *)pftp->pbuf ), -1 );
#ifdef _CVI_
Delay ( 1 );
#else
OSTimeDlyHMSM(0,0,1,0);
#endif
++tick;
if ( tick >= 0x10 ) break;
if ( (tick & 0x7) == 0 )
#ifdef _CVI_
printf ( "\n\n\tI'm super hungry!\n\n" );
#else
if ( pftp->fn ) pftp->fn ( (unsigned char *)"\r\n\tI'm super hungry!\r\n\r\n", strlen ( "\r\n\tI'm super hungry!\r\n\r\n" ), -1 );
#endif
continue; // To Do...
// Here a timeout may be needed! Otherwise endless loop will cause
}
tick = 0;
nbytes += n;
if ( nbytes < pftp->nbufsize ) continue;
#if 0
if ( pftp->fn ) pftp->fn ( (unsigned char *)&nbytes, sizeof ( nbytes ), -1 );
if ( pftp->fn ) pftp->fn ( (unsigned char *)"\r\n", 2, -1 );
n = CRC16_caculate ( pftp->pbuf, nbytes, 0 );
if ( pftp->fn ) pftp->fn ( (unsigned char *)&n, sizeof ( n ), -1 );
if ( pftp->fn ) pftp->fn ( (unsigned char *)"\r\n", 2, -1 );
n = CRC16_caculate ( pftp->pbuf, nbytes - CRC_SIZE, 0 );
if ( pftp->fn ) pftp->fn ( (unsigned char *)&n, sizeof ( n ), -1 );
if ( pftp->fn ) pftp->fn ( (unsigned char *)"\r\n", 2, -1 );
if ( pftp->fn ) pftp->fn ( (unsigned char *)&pftp->pbuf[ nbytes - CRC_SIZE], 2, -1 );
if ( pftp->fn ) pftp->fn ( (unsigned char *)"\r\n", 2, -1 );
#endif
#if 0
if ( pftp->pbuf[ nbytes - CRC_SIZE ] != ((n >> 8) & 0xFF)
|| pftp->pbuf[ nbytes - CRC_SIZE + 1] != ((n >> 0) & 0xFF) )
#else
if ( CRC16_caculate ( pftp->pbuf, nbytes, 0 ) != 0 )// break; // re-send from pftp->nbytes
#endif
{
if ( pftp->fn ) pftp->fn ( (unsigned char *)"\r\n\tCRC Fail\r\n", strlen ( "\r\n\tCRC Fail\r\n" ), -1 );
break;
}
if ( pftp->fn ) pftp->fn ( (unsigned char *)"\r\n\t<Byte recieved from :>\r\n", strlen ( "\r\n\t<Byte recieved from :>\r\n" ), -1 );
if ( pftp->fn ) pftp->fn ( pftp->pbuf, nbytes - CRC_SIZE, offset );
offset += nbytes - CRC_SIZE;
pftp->nbytes += nbytes;
if ( pftp->fn ) pftp->fn ( pftp->pbuf,
sprintf ( (char *)pftp->pbuf,
"\r\n\t<Byte recieved : %08d/%08d/%08d>\r\n", nbytes, pftp->nbytes, pftp->nfilesize ),
-1 );
nbytes = 0;
#ifdef _CVI_
printf ( "\n\n\tByte recieved : %08d/%08d/%08d\n\n", nbytes, pftp->nbytes, pftp->nfilesize );
#endif
}
if ( nbytes > 0 )
if ( CRC16_caculate ( pftp->pbuf, nbytes, 0 ) == 0 )
{
if ( pftp->fn ) pftp->fn ( pftp->pbuf, nbytes - CRC_SIZE, offset );
offset += nbytes - CRC_SIZE;
pftp->nbytes += nbytes;
}
}
socket_close ( pftp->data );
}
}
socket_close ( pftp->ctrl );
GPRS_disconnect ();
#ifdef _CVI_
Delay ( 1 );
#else
OSTimeDlyHMSM(0,0,1,0);
#endif
#ifdef _TW_
GPRS_connect ( (unsigned char *)"INTERNET" );
#else
GPRS_connect ( (unsigned char *)"CMNET" );
#endif
}
return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -