long FtpFileDownload( void *fs, char *url, char *dest, char *fullpath, long length ) { long dataread, datatotal=0; if ( fs ) { void *fp; FILE *ff; char *buffer=NULL; if ( fs && !IsStopped() ) { if ( !(ff = fopen( dest, "wb+" )) ) { StatusSetf( "Cannot open local file %s for writing.", dest ); return -2; } fp = (void*)FtpOpen( fs, fullpath, 'R' ); if ( fp ) { if ( !length ) length = FtpFileGetSize( url ); StatusSetID( IDS_DOWNLOADING, fullpath ); buffer = (char*)malloc( 1024*4 ); if ( length && buffer ) { long writesize; dataread = 1; while( dataread>0 && buffer && !IsStopped() ) { //sprintf( msg, "Downloading %s...%d KB (%.2f%%)", fullpath, (datatotal)/1024, (100*((datatotal)/(float)length)) ); StatusSetID( IDS_DOWNLOADINGKB, fullpath, (datatotal)/1024, (100*((datatotal)/(float)length)) ); dataread = NetRead( fp, buffer, 1024*1 ); datatotal+= dataread; if ( dataread ){ writesize = fwrite( buffer, 1, dataread, ff ); if ( writesize == 0 ){ dataread = 0; datatotal = -2; } } } free( buffer ); } FtpClose( fp ); } else { const char *msg = NetworkErr( NULL ); if ( !msg ) msg = "Maybe path does not exist?"; ErrorMsg( "Cannot open ftp file %s\nbecause ...\n%s", fullpath, msg ); datatotal = -1; } fclose( ff ); } if ( IsStopped() ) { StatusSetID( IDS_DOWNLOADSTOP ); remove( dest ); datatotal = -1; } } return datatotal; }
//***************************************************************************** // Function : DownloadFtp // Description : App download over FTP. // Input : N/A // Return : N/A // Note : N/A // Globals Changed : N/A //***************************************************************************** BOOLEAN DownloadFtp(void) { int i, wr_ok=0, gf=0; //BYTE lon=0,IP[4]; char filename[32]/*,*tem*/; struct MW_TCP_PORT port2; struct FTPPARAM ftpparam; BYTE ipHost[4]; BYTE puerto[6+7]; BYTE usuario[21]; BYTE clave[21]; // memset(Toptgral.downloadIP,0x00,sizeof(Toptgral.downloadIP)); // strcpy(Toptgral.downloadIP,"174.142.211.61"); //printf("paso 1");getch(0); // strcpy(Toptgral.downloadIP,"210.3.29.84"); // download auth if (DownloadAuth() < 0) { graphicWindow("FALLA auth"); // centerS(3,1,21,"Proceso de"); // centerS(4,1,21,"Autenticacion"); // centerS(6,1,21,"Fallo !!!"); printf("\nAUTH ERR"); // getch(0); Delay10ms(500); return TRUE; } //GetFtpParam(&ftpparam); /*printf("\fIPftp:|%d %d %d %d|", ftpparam.ipHost[0], ftpparam.ipHost[1], ftpparam.ipHost[2], ftpparam.ipHost[3]); GetChar(); printf("\fPtoftp:|%d|", ftpparam.portFtp); GetChar(); printf("\fUsuftp:|%s|", ftpparam.gUsername); GetChar(); printf("\fPassftp:|%s|", ftpparam.gPassword); GetChar();*/ memset(usuario,0x00,sizeof(usuario)); memcpy(&usuario[1], gFtpParam.userID , strlen(gFtpParam.userID)); usuario[0] = strlen(gFtpParam.userID); memset(clave,0x00,sizeof(clave)); split(&clave[1], gtoken_97.worKeyPin, 2); clave[0] = 4; memset(puerto,0x00,sizeof(puerto)); sprintf(puerto, "%d", gFtpParam.portFtp); memset(ipHost,0x00,sizeof(ipHost)); memcpy(ipHost, gFtpParam.ipHost, 4); if( flagFtpManual == TRUE ) { setApn( ); //OJO NOHORA 13 - MAR 2013// printf("\f"); DispLineMW("INFORMACION", MW_LINE1, MW_CENTER|MW_SMFONT); DispLineMW("IP:",MW_LINE2,MW_CENTER|MW_SMFONT); EditIp(ipHost, MW_LINE3); //DispLineMW(buf, MW_LINE3, MW_CENTER|MW_SPFONT); //printf("\f"); DispLineMW("Puerto:", MW_LINE4, MW_CENTER|MW_SMFONT); GetString(puerto, MW_LINE5, 5); //DispLineMW(puerto, MW_LINE5, MW_CENTER|MW_SPFONT); //printf("\f"); DispLineMW("Usuario:", MW_LINE6, MW_CENTER|MW_SMFONT); //memcpy(&usuario[1], gUsername, ); APM_GetKbdSpectra(ALPHA_INPUT_NEW + MW_SPFONT + MW_LINE7 + ECHO, IMIN(1) + IMAX(16), usuario); //DispLineMW(gUsername, MW_LINE7, MW_CENTER|MW_SPFONT); //printf("\f"); DispLineMW("Clave:", MW_LINE8, MW_CENTER|MW_SMFONT); APM_GetKbdSpectra(ALPHA_INPUT_NEW + MW_SPFONT + MW_LINE9 + ECHO, IMIN(1) + IMAX(16), clave); //DispLineMW(gPassword, MW_LINE9, MW_CENTER|MW_SPFONT); //WaitKey(1000, 0); DispLineMW( "REALMENTE", MW_LINE3, MW_CENTER|MW_SMFONT|MW_CLRDISP ); DispLineMW( "DESEA EJECUTAR", MW_LINE4, MW_CENTER|MW_SMFONT ); DispLineMW( "LA DESCARGA FTP ?", MW_LINE5, MW_CENTER|MW_SMFONT ); if( SiNo() != 2 ) // 2 = Press Enter return FALSE; AcceptBeep(); memset(&ftpparam, 0, sizeof(struct FTPPARAM)); memcpy(ftpparam.ipHost, ipHost, 4); ftpparam.portFtp = atoi(puerto); memcpy(ftpparam.userID, &usuario[1], usuario[0]); memcpy(ftpparam.Password, &clave[1],clave[0]); } else{ memset(&ftpparam, 0, sizeof(struct FTPPARAM)); memcpy(ftpparam.ipHost, gToken_96.ipTeleCarga, 4); memset(puerto, 0x00, sizeof(puerto)); memcpy(puerto, gToken_96.puertoTeleCarga, 5); ftpparam.portFtp = atoi(puerto); memcpy(ftpparam.userID, "CNB", 3); split(ftpparam.Password, gtoken_97.worKeyPin, 2); } memcpy(&gFtpParam, &ftpparam, sizeof(struct FTPPARAM)); SaveFtpParam(&gFtpParam); /*printf("\fIPftp2:|%d %d %d %d|", ftpparam.ipHost[0], ftpparam.ipHost[1], ftpparam.ipHost[2], ftpparam.ipHost[3]); GetChar(); printf("\fPtoftp2:|%d|", ftpparam.portFtp); GetChar(); printf("\fUsuftp2:|%s|", ftpparam.gUsername); GetChar(); printf("\fPassftp2:|%s|", ftpparam.gPassword); GetChar();*/ // printf("\fusuario: %s",gUsername);WaitKey(5000,0); // printf("\fpass: %s",gPassword);WaitKey(5000,0); /* DispPutStr("\x12\x1B\x05\x01""Username:\x5\n"); GetString(gUsername, MW_LLINE6, MW_LCOL1, 15); DispPutStr("\x12\x1B\x05\x01Pwd:\x5\n"); GetString(gPassword, MW_LLINE6, MW_LCOL1, 15); */ // memset(gUsername,0x00,sizeof(gUsername)); // strcpy(gUsername,"wwtest"); // strcpy(gUsername,"t800"); // memset(gPassword,0x00,sizeof(gPassword)); // strcpy(gPassword,"1234"); // strcpy(gPassword,"HasFTP.."); // IP[0]=67;IP[1]=210;IP[2]=244;IP[3]=44; /* */ /* IP[0]=200; IP[1]=26; IP[2]=147; IP[3]=234; memcpy(&port2.d_ip, IP, 4); port2.w_port = 21;*/ // port.w_port = Toptgral.downloadPort; ////////////////// /*IP[0]=210; IP[1]=3; IP[2]=29; IP[3]=84;*/ //memcpy(&port2.d_ip, IP, 4); memcpy(&port2.d_ip, gFtpParam.ipHost, 4); port2.w_port = gFtpParam.portFtp; //dumpMemory((BYTE *)&port2,100,1); //printf("\f%s y %s",gUsername,gPassword);getch(0); //////////////// port2.b_sslidx = 0; port2.b_option = 0; // if (GetPPPStatus() != MW_PPP_PHASE_READY){ // PPPSetup(); // PPPConnect(); // } printf("\f"); APM_ConnectPPP(TRUE); // // if (GetPPPStatus() != MW_PPP_PHASE_READY) // return; port2.b_option |= MW_TCP_PPP_MODE; //port2.b_option |= 0x13; /* if (GetPPPStatus() == MW_PPP_PHASE_READY) { int option = 1; PutCharMW(MW_LPUSH_CURSOR); ClrBelow(MW_LLINE2); option = ToggleOption("\x11\x1B\x05\x01Use PPP Channel?", KTrueFalse, option); if (option == -1) return; if (option == 1) { port.b_option |= MW_TCP_PPP_MODE; } PutCharMW(MW_LPOP_CURSOR); } */ /* if ((port.b_option & MW_TCP_PPP_MODE) == 0) { // enable LAN interface if needed if ((bs_lan_status(K_BsLanStatus) & 0x02) == 0) { bs_lan_open(); while ((bs_lan_status(K_BsLanStatus) & 0x02) != 1) { ShowStatus("Enabling LAN...\x05", STS_ERROR); SleepMW(); } } // check LAN cable if (!LanCableInsertedMW()) { ShowStatus("Check Lan Cable!\x05", STS_ERROR); WaitKey(); return; } } */ strcpy(filename, DEV_FTP1); gFtpHandle = OpenMW(filename, 0); if (gFtpHandle==-1) { graphicWindow("FALLA serv"); // centerS(4,1,21,"No se pudo"); // centerS(5,1,21,"Abrir servidor"); //DispPutStr("\x12\x1B\x08\x01\x1C""FTP:Open Fail!\x05\x1D"); CloseAll (); CloseMW(gFtpHandle); // getch(1000); Delay10ms(1000); return TRUE; } memset(gFileList, 0, sizeof(gFileList)); gNumOfFiles = 0; //port2.b_option |= MW_TCP_NO_CLOSE_WAIT; port2.b_option |= 0x13; //ClrBelow(MW_LLINE5); do { // Open FTP if ( FtpOpen(&port2, gFtpParam.userID, gFtpParam.Password ) < 0 ) break; // Get list file if (GetListFile() < 0) break; // Download files for (i=0; i<gNumOfFiles; i++) { gf=GetFile(i); //printf("\f%d\n %d", gf, i);APM_WaitKey(3000,0); if (gf < 0) break; if(gf == 1) continue; if (WriteFileToApp(i) == K_APPDL_RESULT_OKAY) wr_ok = 1; // reboot even have 1 app written } } while (0); // DispPutStr("\x1B\x08\x01\x1C""Closing...\x05\x1D"); CloseAll (); CloseMW(gFtpHandle); // os_disp_putc(MW_LSEL_SMFONT); // os_disp_putc(MW_LREV_ON); //centerS( 8, 1,16, " " ); //printf(" Fin de Proceso \n" ); // os_disp_putc(MW_LSEL_SPFONT); // os_disp_putc(MW_LREV_OFF); //ShowMsgWait("All done.", TRUE); /*if (wr_ok) { // clear_area_screen(3,3,7,20,FALSE,FALSE); //printf( " Reseteando " ); //ResetMW(); }*/ return TRUE; }
// -2 cant make dir long FtpFileUpload( void *fs, char *localfilename, char *remotepath, char *file ) { long length=0, failedmkdir=FALSE, datadone; if ( fs && !IsStopped() ) { void *hFtpFile; FILE *ff; long dataleft, dataread, perc; char *buffer=NULL; char msg[500]; if ( !(ff = fopen( localfilename, "rb" )) ) { StatusSetID( IDS_ERR_FILEFAILED, localfilename ); return FTPERR_NOFILE; } if ( fs && ff ) { char dir[256], *p; static char lastdir[256]; int tries = 0; // Convert all Windows slashes to Unix ones. p = remotepath; while( *p ) { if ( *p == '\\' ) *p = '/'; p++; } // Try to create the remote directories first. if ( p ) { // Try to open remote file, if we cant make the remote dir. while( (hFtpFile = (void*)FtpOpen( fs, remotepath, 'W' )) == NULL && tries<5 ) { PathFromFullPath( remotepath, dir ); if ( strcmp( dir, lastdir ) ){ //long l=mystrcpy( lastdir, dir ); //if ( dir[l-1] == '/' ) dir[l-1]=0; sprintf( msg, "Making dir %s...", dir ); StatusSet( msg ); if ( !FtpMakeDir( fs, dir ) ) { failedmkdir = TRUE; OutDebugs( "Cannot create remote ftp directory %s", dir ); } else { failedmkdir = FALSE; } } tries++; OutDebug( "Trying again to write to server..." ); } } // If remote file is opened. if ( hFtpFile ) { StatusSetID( IDS_UPLOADINGTO, remotepath ); const long read_size = (4*1024); datadone = 0; length = dataleft = (long)GetFileLength( localfilename ); if ( length ) buffer = (char*)malloc( read_size ); if ( buffer ) { while( dataleft>0 && buffer && !IsStopped() ) { dataread = fread( buffer, 1, read_size, ff ); if ( dataread ) NetWrite( hFtpFile, buffer, dataread ); dataleft -= dataread; datadone += dataread; // ------------------------------------------------------ perc = (long)(100*((length-dataleft)/(float)length)); sprintf( msg, "Uploading %s (%d bytes %d%%)", remotepath, datadone, perc ); StatusSet( msg ); // ------------------------------------------------------ } if ( datadone == length ) OutDebug( "Uploaded complete." ); free( buffer ); } FtpClose( hFtpFile ); OutDebug( "Ftp File Closed" ); } } if ( ff ) fclose( ff ); } if ( failedmkdir ) return FTPERR_CANTMAKEDIR; if ( IsStopped() ) return FTPERR_STOPPED; if ( datadone == length ) return FTPERR_COMPLETE; return FTPERR_COMPLETEFAIL; }