예제 #1
0
파일: FTPlib.c 프로젝트: Hotfirenet/Tuto
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);
}
예제 #2
0
long FTPFileSize(TCP_SOCKET cmdSock, char fileName[])
{
	char cmd[strlen(fileName) + 10], len[12];
	int res;
	strcpy(cmd, "SIZE ");
	strcat(cmd, fileName);
	
	FTPRxFlush(cmdSock, 100);
	res = FTPSendCmd(cmdSock, cmd, len, 12);
	if (res < 0)
		return res;
	_dbgwrite("FTPFileSize:");
	_dbgwrite(len);
	if (res == 550)
		return FTP_FILE_NOT_FOUND;
	if (res == 213)
		return atol(len);
	return FTP_ERR_WRONG_ANSWER;
}
예제 #3
0
파일: FTPlib.c 프로젝트: Hotfirenet/Tuto
/**
 * 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);
}
예제 #4
0
파일: FTPlib.c 프로젝트: Hotfirenet/Tuto
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;
}
예제 #5
0
파일: FTPlib.c 프로젝트: Hotfirenet/Tuto
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;
}
예제 #6
0
파일: FTPlib.c 프로젝트: Hotfirenet/Tuto
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;
}
예제 #7
0
파일: FTPlib.c 프로젝트: Hotfirenet/Tuto
/**
 * Open a PassiveMode data exchange between Flyport (Client) and the ServerName
 * \param sockpasv - The handle of the socket for commands exchange.
 * \param ServerName - The server data IP returned by the FTP Server
 * \return The TCP_SOCKET to use for data transfer in Passive Mode.
 */
TCP_SOCKET FTPClientPasv(TCP_SOCKET sockpasv, char ServerName[])
{	
	unsigned long int dataport=0;
	char *word;
	char word1[10];
	char buffertot[100];
	char servReply[30];
	int counter = 0;
		
	FTPRxFlush(sockpasv, 100);
	BYTE strpasv[]={"pasv\r\n"};
	FTPMultiWrite(sockpasv,strpasv,6);
	
	while (FTPRxLen(sockpasv) == 0)
	{
		vTaskDelay(10);
		counter++;
		if (counter == 50)
			break;
	}
	counter = 0;
	
	int indsum=0;
	while (FTPRxLen(sockpasv)>0)
	{
		int toread = FTPRxLen(sockpasv);
		FTPRead(sockpasv , (char *) (buffertot+indsum) , toread);
		indsum += toread;
		while (FTPRxLen(sockpasv) == 0)
		{
			vTaskDelay(10);
			counter++;
			if (counter == 20)
				break;
		}		
	}
	
	word = strtok(buffertot,",()");

	int indsub=1;
	while (word != NULL)
	{
		word = strtok(NULL, ",()");
		if (indsub == 1)
		{
			sprintf(servReply, "%s", word);
			strcat(servReply, ".");
		}
		if (indsub == 2)
		{
			strcat(servReply, word);
			strcat(servReply, ".");
		}
		else if (indsub == 3)
		{
			strcat(servReply, word);
			strcat(servReply, ".");
		}
		else if (indsub == 4)
		{
			strcat(servReply, word);
		}
		else if (indsub == 5)
		{
			dataport=atoi(word);
			dataport=dataport*256;
			int xy;
			xy=sprintf(word1,"%li",dataport);			
		}
		else if (indsub == 6)
		{
			dataport += atoi(word);
			sprintf(word1,"%li",dataport);
		}
		indsub++;
	}

	xFrontEndStatRet = 2;	
	ToSend=20;
	sprintf(ServerName, "%s", servReply);
	return TCPGenericOpen(ServerName, TCP_OPEN_RAM_HOST , word1 , 4);	
}