void FTPStreamClose() { if (FTPisConn(dataSocket)) { FTPClose(dataSocket); } dataSocket = INVALID_SOCKET; streamStat = FTP_STREAM_NOT_CONN; }
/** * Performs a check on the remote FTP server for the existence of the specified file. Only the FTP command * socket must be provided, the FTP data connection is managed internally by the framework. * \param cmdSock - The handle of the socket for commands exchange. * \param fileToCheck - the string containing the name of the file to check on the server. * \return an int containing the value FTP_FILE_EXIST (0x00) if the file is found * \return an error code. Detailed list of error code can be found at the beginning of FTP chapter. */ int FTPFileCheck(TCP_SOCKET cmdSock, char fileToCheck[]) { int servReply; char code[5]; // #0 - Checking FTP socket connection if (!FTPisConn(cmdSock)) return FTP_SOCK_NOT_CONNECTED; // #1 - Sending SIZE command FTPRxFlush(cmdSock, 100); FTPMultiWrite(cmdSock, (BYTE*)"SIZE ",5); FTPMultiWrite(cmdSock, (BYTE*)fileToCheck, strlen(fileToCheck)); FTPMultiWrite(cmdSock, (BYTE*)"\r\n", 2); // #2 - Reading answer from server servReply = FTPAnswer(cmdSock, code); if (servReply != 0) { if (FTPisConn(cmdSock)) { errHandling(&cmdSock); return FTP_ERR_SERV_TIMEOUT; } else { errHandling(&cmdSock); return FTP_ERR_SERV_DISCONNECTED; } } // #3 - Answer decoding if ( strcmp(code,"550") == 0 ) return FTP_FILE_NOT_FOUND; else if (strcmp(code,"213") == 0 ) return FTP_FILE_EXIST; return atoi(code); }
long FTPStreamWrite(char strWrite[], long toWrite) { long len = 0; DWORD tick1, tick2; if ( (FTPisConn(dataSocket)) && (streamStat == FTP_STREAM_WRITING) ) { tick1 = TickGetDiv64K(); while (len < toWrite) { len += FTPWrite(dataSocket, (BYTE*)&strWrite[len], toWrite - len); // Checking timeout on writing operation tick2 = TickGetDiv64K(); if ((tick2 - tick1) > 5) { // Timeout occured during writing, a check on data connection is required if (!FTPisConn(dataSocket)) { debug("FTPStreamWrite: timeout by server disconnection\n"); errHandling(&dataSocket); streamStat = FTP_STREAM_NOT_CONN; return len; } else { debug("FTPStreamWrite: timeout\n"); return len; } } } return len; } else if (!FTPisConn(dataSocket)) return FTP_DATA_NO_CONNECTED; else return FTP_STREAM_INVALID_OP; }
int FTPSendCmd(TCP_SOCKET cmdSock, char *cmdStr, char* res, int resLen) { int servReply; char code[5]; FTPRxFlush(cmdSock, 100); int wBytes = 0; BOOL chkWrite = TRUE; // Sending FTP command wBytes = FTPMultiWrite(cmdSock, (BYTE*)cmdStr, strlen(cmdStr)); if (wBytes < strlen(cmdStr)) chkWrite = FALSE; wBytes = FTPMultiWrite(cmdSock, (BYTE*)"\r\n", 2); if (wBytes < 2) chkWrite = FALSE; if (!chkWrite) { if (FTPisConn(cmdSock)) return FTP_ERR_TX; else return FTP_ERR_SERV_DISCONNECTED; } // Reading answer from server servReply = FTPAnswer(cmdSock, code); if (servReply != 0) { return FTP_ERR_SERV_TIMEOUT; } if (res != NULL) { FTPRxRead(cmdSock, 100, res, resLen); } else FTPRxFlush(cmdSock, 100); return atoi(code); }
long FTPStreamRead(char dest[], int len, BYTE timeout) { long count = 0; int toRead; DWORD tick1, tick2; if ( (FTPisConn(dataSocket)) && (streamStat == FTP_STREAM_READING) ) { tick1 = TickGetDiv64K(); while (count < len) { toRead = FTPRxLen(dataSocket); // Resizing bytes to read according to buffer size if (toRead > (len - count)) toRead = len - count; FTPRead(dataSocket, &dest[count], toRead); count += toRead; streamRBytes += toRead; // No data to read, checking timeout and chars read if (toRead == 0) { // check on file ending, if file is not finished, checking timeout if (streamRBytes == streamLen) { debug("FTPStreamRead: EOF reached\n"); streamStat = FTP_STREAM_EOF; return count; } tick2 = TickGetDiv64K(); IOPut(o4, toggle); if ( (tick2 -tick1) > timeout) { // Timeout occured during reading, a check on data connection is required if (!FTPisConn(dataSocket)) { debug("FTPStreamRead: timeout by server disconnection\n"); errHandling(&dataSocket); streamStat = FTP_STREAM_NOT_CONN; return count; } else { debug("FTPStreamRead: timeout\n"); return count; } } } /* else { sprintf(dbg, "count:%d\n", count); UARTWrite(1, dbg); sprintf(dbg, "toRead:%d\n", toRead); UARTWrite(1, dbg); } */ } return count; } else if (!FTPisConn(dataSocket)) return FTP_DATA_NO_CONNECTED; else return FTP_STREAM_INVALID_OP; }
int FTPStreamOpen(TCP_SOCKET cmdSock, char fileName[], char mode[]) { // Stream opening started only if stream is not connected if ( (dataSocket == INVALID_SOCKET) && (streamStat == FTP_STREAM_NOT_CONN) ) { char pasvBuff[16 + strlen(fileName)]; int cnt = 0, servReply; char code[5]; // Check on command socket connection if ( (!FTPisConn(cmdSock))|| (cmdSock == INVALID_SOCKET) ) return FTP_SOCK_NOT_CONNECTED; // Reading file size if (strcmp(mode, RETR) == 0) { debug("StreamOpen: RETR\n"); streamLen = FTPFileSize(cmdSock, fileName); if (streamLen < 0) return streamLen; } // Connection in passive mode dataSocket = FTPClientPasv(cmdSock, pasvBuff); // Waiting for data socket connection while (!FTPisConn(dataSocket)) { cnt++; vTaskDelay(10); if (cnt == 30) { errHandling(&dataSocket); return FTP_DATA_NO_CONNECTED; } } // Append file command APPE FTPMultiWrite(cmdSock, (BYTE*)mode,5); FTPMultiWrite(cmdSock, (BYTE*)fileName, strlen(fileName)); FTPMultiWrite(cmdSock, (BYTE*)"\r\n", 2); // Reading answer from server servReply = FTPAnswer(cmdSock, code); if (servReply != 0) { errHandling(&dataSocket); return FTP_ERR_SERV_TIMEOUT; } FTPRxFlush(cmdSock, 100); if ( ( strcmp(code,"150") != 0 ) && ( strcmp(code,"125") != 0 ) ) return atoi(code); if ( (strcmp( mode, APPE) == 0) || (strcmp( mode, STOR) == 0) ) streamStat = FTP_STREAM_WRITING; else if(strcmp( mode, RETR) == 0) { streamRBytes = 0; streamStat = FTP_STREAM_READING; } return FTP_CONNECTED; } else return FTP_STREAM_INVALID_OP; }
static int FTPwFile(TCP_SOCKET cmdSock, char fileName[], char appStr[], char wMode[]) { dataSocket = INVALID_SOCKET; char pasvBuff[16 + strlen(fileName)]; // buffer for IP address int cnt = 0, servReply, wBytes = 0; char code[5]; BOOL chkWrite = TRUE; // #0 - Checking FTP socket connection if (!FTPisConn(cmdSock)) return FTP_SOCK_NOT_CONNECTED; // #1 - Connection in passive mode dataSocket = FTPClientPasv(cmdSock, pasvBuff); // Waiting for data socket connection while (!FTPisConn(dataSocket)) { cnt++; vTaskDelay(10); if (cnt == 30) { errHandling(&dataSocket); return FTP_DATA_NO_CONNECTED; } } // #2 - Append file command APPE, check on TX chars and answer handling wBytes = FTPMultiWrite(cmdSock, (BYTE*)wMode,5); if (wBytes < 5) chkWrite = FALSE; wBytes = FTPMultiWrite(cmdSock, (BYTE*)fileName, strlen(fileName)); if (wBytes < strlen(fileName)) chkWrite = FALSE; wBytes = FTPMultiWrite(cmdSock, (BYTE*)"\r\n", 2); if (wBytes < 2) chkWrite = FALSE; if (!chkWrite) { if (FTPisConn(cmdSock)) return FTP_ERR_TX; else return FTP_ERR_SERV_DISCONNECTED; } // Reading answer from server servReply = FTPAnswer(cmdSock, code); if (servReply != 0) { if (FTPisConn(cmdSock)) { errHandling(&cmdSock); return FTP_ERR_SERV_TIMEOUT; } else { errHandling(&cmdSock); return FTP_ERR_SERV_DISCONNECTED; } } FTPRxFlush(cmdSock, 100); if ( strcmp(code,"150") != 0 ) { if ( strcmp(code,"550") == 0 ) return FTP_FILE_ERROR; else return FTP_ERR_WRONG_ANSWER; } // #3 - Sending data to append and check on TX chars wBytes = FTPMultiWrite(dataSocket, (BYTE*)appStr, strlen(appStr)); if (wBytes < strlen(appStr)) { if (FTPisConn(cmdSock)) { errHandling(&dataSocket); return FTP_ERR_TX; } else { errHandling(&dataSocket); return FTP_ERR_SERV_DISCONNECTED; } } errHandling(&dataSocket); // Reading answer from server servReply = FTPAnswer(cmdSock, code); if (servReply != 0) { if (FTPisConn(dataSocket)) { errHandling(&dataSocket); return FTP_ERR_SERV_TIMEOUT; } else { errHandling(&dataSocket); return FTP_ERR_SERV_DISCONNECTED; } } errHandling(&dataSocket); // #4 - Returning 0 if succeded, otherwise the server from code if ( strcmp(code,"226") != 0 ) return FTP_ERR_TX_NOT_OK; return 0; }
int FTPConnect(TCP_SOCKET *FTPConn, char *ftp_addr, char ftp_port[], char ftp_usr[], char ftp_pwd[]) { int err_count = 0; int code_rep = 0, flush_rep = 0; debug("\r\nServer IP:"); debug(ftp_addr); debug("\r\nServer port:"); debug(ftp_port); debug("\n"); // #1 - SOCKET OPENING: A new FTP socket is open to connect to the remote server if (FTPisConn(*FTPConn)) { return FTP_CONNECTED; } *FTPConn = INVALID_SOCKET; *FTPConn = FTPClientOpen(ftp_addr, ftp_port); vTaskDelay(50); // Socket creation check: if an internal error occured, // Flyport retries up to 3 times to create the socket while ( (*FTPConn == INVALID_SOCKET) && (err_count < 3) ) { err_count++; vTaskDelay(20); errHandling(FTPConn); vTaskDelay(10); *FTPConn = FTPClientOpen(ftp_addr, ftp_port); vTaskDelay(20); } // EXCEPTION HANDLING: socket was not created if (*FTPConn == INVALID_SOCKET) return FTP_ERR_NOT_CREATED; // Socket is created, now let's check the connection // Flyport will wait up to 10 seconds for connection with server err_count = 0; while ( (!FTPisConn(*FTPConn)) && (err_count < 20) ) { debug("\r\nServer searching..."); vTaskDelay(50); err_count++; } debug("\r\n"); // EXCEPTION HANDLING: server not found if (!FTPisConn(*FTPConn)) { errHandling(FTPConn); return FTP_ERR_SERV_NOT_FOUND; } debug("\r\nFTPConnect: SERVER FOUND\r\n"); // Reading the response code from the server // (expected response code: 220) char code[5]; code_rep = FTPAnswer(*FTPConn, code); // EXCEPTION HANDLING: timeout and bad server answer if (code_rep != 0) { errHandling(FTPConn); return FTP_ERR_SERV_TIMEOUT; } if (strcmp(code, "220") != 0) { errHandling(FTPConn); return atoi(code); } else debug("\r\nFTPConnect: SERVER ANSWERED\r\n"); flush_rep = FTPRxFlush(*FTPConn, 300); vTaskDelay(50); // #2 - SENDING USERNAME TO SERVER FTPMultiWrite(*FTPConn, (BYTE*)"USER ", 5); FTPMultiWrite(*FTPConn, (BYTE*)ftp_usr, strlen(ftp_usr)); FTPMultiWrite(*FTPConn, (BYTE*)"\r\n", 2); code_rep = FTPAnswer(*FTPConn, code); // EXCEPTION HANDLING: server timeout if (code_rep != 0) { if (FTPisConn(*FTPConn)) { errHandling(FTPConn); return FTP_ERR_SERV_TIMEOUT; } else { errHandling(FTPConn); return FTP_ERR_SERV_DISCONNECTED; } } // EXCEPTION HANDLING: wrong answer from server if ( strcmp(code,"331") != 0 ) { errHandling(FTPConn); return atoi(code); } else debug("USER OK\r\n"); flush_rep = FTPRxFlush(*FTPConn, 100); // #3 - SENDING PASSWORD TO SERVER FTPMultiWrite(*FTPConn, (BYTE*)"PASS ", 5); if (ftp_pwd != FTP_NO_PASS) FTPMultiWrite(*FTPConn, (BYTE*)ftp_pwd, strlen(ftp_pwd)); FTPMultiWrite(*FTPConn, (BYTE*)"\r\n", 2); code_rep = FTPAnswer(*FTPConn, code); // EXCEPTION HANDLING: server timeout if (code_rep != 0) { if (FTPisConn(*FTPConn)) { errHandling(FTPConn); return FTP_ERR_SERV_TIMEOUT; } else { errHandling(FTPConn); return FTP_ERR_SERV_DISCONNECTED; } } // EXCEPTION HANDLING: wrong password and unknow answer from server if ( strcmp(code,"230") != 0 ) { if (strcmp(code,"530") == 0 ) { errHandling(FTPConn); return FTP_ERR_WRONG_LOGIN; } else { errHandling(FTPConn); return atoi(code); } } else debug("PASSWORD OK\r\n"); flush_rep = FTPRxFlush(*FTPConn, 100); // Everything went OK, returning connected status return FTP_CONNECTED; }
long FTPStreamRead(char dest[], int len, BYTE timeout) { long count = 0, count2 = 0; int toRead; DWORD tick1, tick2; if (streamStat == FTP_STREAM_READING) { tick1 = TickGetDiv64K(); while (count < len) { count2 = 0; toRead = FTPRxLen(dataSocket); while ((toRead < len) && (toRead + streamRBytes < streamLen)) { toRead = FTPRxLen(dataSocket); count2++; if (count2 == 3) break; } // Resizing bytes to read according to buffer size if (toRead > (len - count)) toRead = len - count; FTPRead(dataSocket, &dest[count], toRead); count += toRead; streamRBytes += toRead; // No data to read, checking timeout and chars read if (toRead == 0) { // check on file ending, if file is not finished, checking timeout if (streamRBytes == streamLen) { _dbgwrite("FTPStreamRead: EOF reached\r\n"); streamStat = FTP_STREAM_EOF; return count; } tick2 = TickGetDiv64K(); if ( (tick2 -tick1) > timeout) { // Timeout occured during reading, a check on data connection is required if (!FTPisConn(dataSocket)) { _dbgwrite("FTPStreamRead: timeout by server disconnection\r\n"); errHandling(&dataSocket); streamStat = FTP_STREAM_NOT_CONN; return count; } else { _dbgwrite("FTPStreamRead: timeout\r\n"); return count; } } } } return count; } else if (!FTPisConn(dataSocket)) { errHandling(&dataSocket); return FTP_DATA_NO_CONNECTED; } else return FTP_STREAM_INVALID_OP; }