⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ftp.c

📁 linux的多线程下载工具。基于gtk界面。
💻 C
📖 第 1 页 / 共 2 页
字号:
/* ftp.c
 *
 * Copyright (C) 2004-2004 Wang Xiaoguang (Chice) <chice_wxg@hotmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 *
 * Authors: Wang Xiaoguang (Chice) <chice_wxg@hotmail.com>
 */



#include "ftp.h"
#include "../dld_log.h"
#include "../dld_language.h"
#include "../dld_config.h"
#include "../dld_task.h"
#include "../dld_taskmgr.h"
#include "../dld_thread.h"
#include "../dld_ui_callback.h"



/////////////////////////////////
//FTP Protocol Structs
/////////////////////////////////

enum
{
	FTP_STATUS_WAITING = 0,
	FTP_STATUS_WELCOME,
	FTP_STATUS_WELCOME_OK,
	FTP_STATUS_USER,
	FTP_STATUS_USER_OK,
	FTP_STATUS_PASS,
	FTP_STATUS_PASS_OK,
	FTP_STATUS_BINARY,
	FTP_STATUS_BINARY_OK,	
	FTP_STATUS_SIZE,
	FTP_STATUS_SIZE_OK,
	FTP_STATUS_RESET,
	FTP_STATUS_RESET_OK,
	FTP_STATUS_PASV,
	FTP_STATUS_PASV_OK,
	FTP_STATUS_RETR,
	FTP_STATUS_RETR_OK,
	FTP_STATUS_RETR_OVER
};


typedef struct _FtpTaskData
{
	INT dwTmp;
}FtpTaskData;

typedef struct _FtpTaskThread
{
	//Public Member ... (The same as other TaskThread)
	DLD_TASKTHREAD_PUBLICMEMBER;
	
	//Private Member
	INT    cmdstatus;
	SOCKET connsocket;
	SOCKET datasocket;
	BUFFER *buffer;
}FtpTaskThread;


THREADRET ftp_thread_main(POINTER param);
THREADRET ftp_thread_download(POINTER param);//DldTaskThread *ftp_thread_new(DldTask *task);

//init the data of ftp protocol ( task->protocoldata )// NOTHING TO DO NOW!!!!!!!!INT ftp_protocol_data_init(DldTask *task){/*	FtpProtocolData *data = NULL;	data = os_new(FtpProtocolData, 1);	task->protocoldata = data;*/	return DLD_ERR_OK;}//destroy the data of ftp protocol//   free finishedblocks(LIST, ftp_DATA_BLOCK)//   free protocoldataINT ftp_protocol_data_destroy(DldTask *task){
	/*	FtpProtocolData *data;	data = task->protocoldata;	//Do Some Free here!		task->protocoldata = os_free(task->protocoldata);*/	return DLD_ERR_OK;}
////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////

DldTaskThread *ftp_taskthread_new(DldTask *task)
{
	FtpTaskThread *taskthread;

	taskthread = os_new(FtpTaskThread, 1);
	taskthread->status = DLD_TASKTHREAD_STOPPED;
	taskthread->protocoltype = DLD_PROTOCOL_FTP;
	taskthread->parenttask = task;
	taskthread->block = NULL;
	taskthread->fp = NULL;
	taskthread->timeouttick = os_tick_new(g_config.timeout, FALSE);
	taskthread->retrytick = os_tick_new(g_config.retrydelay, FALSE);
	taskthread->log = dld_log_new();

	taskthread->buffer = buffer_new(4096);
	taskthread->connsocket = 0;
	taskthread->datasocket = 0;
	taskthread->cmdstatus = FTP_STATUS_WAITING;

	return (DldTaskThread *)taskthread;
}


DldTaskThread *ftp_taskthread_free(DldTaskThread *dldtaskthread)
{
	FtpTaskThread *taskthread = (FtpTaskThread *)dldtaskthread;
	
	if(!taskthread) return NULL;

	taskthread->block = os_free(taskthread->block);
	if(taskthread->fp)
	{
		fclose(taskthread->fp);
		taskthread->fp = NULL;
	}

	taskthread->log = dld_log_free(taskthread->log);
	taskthread->parenttask = NULL;
	taskthread->timeouttick = os_tick_free(taskthread->timeouttick);
	taskthread->retrytick = os_tick_free(taskthread->retrytick);

	if(taskthread->connsocket)
	{
		os_socket_tcp_close(taskthread->connsocket);
		taskthread->connsocket = 0;
	}

	if(taskthread->datasocket)
	{
		os_socket_tcp_close(taskthread->datasocket);
		taskthread->datasocket = 0;

	}
	taskthread->buffer = buffer_free(taskthread->buffer);
	
	taskthread = os_free(taskthread);
	return NULL;
}

VOID ftp_task_free_taskthreads(DldTask *task)
{
	if(!task) return;
	
	while(task->taskthreads)
	{
		ftp_taskthread_free((DldTaskThread *)(task->taskthreads->data));
		task->taskthreads = list_remove_nth(task->taskthreads, 0);
	}

}


INT ftp_taskthread_start(DldTaskThread *dldtaskthread)
{
	FtpTaskThread *taskthread = (FtpTaskThread *)dldtaskthread;
	DldTask *task;

	task = (DldTask *)taskthread->parenttask;

	taskthread->status = DLD_TASKTHREAD_RUNNING;

	if(!taskthread->fp) taskthread->fp = fopen(task->localfile->str, "rb+");
	if(!taskthread->fp) taskthread->fp = fopen(task->localfile->str, "wb+");

	if(taskthread->connsocket)
	{
		os_socket_tcp_close(taskthread->connsocket);
		taskthread->connsocket = 0;
	}

	if(taskthread->datasocket)
	{
		os_socket_tcp_close(taskthread->datasocket);
		taskthread->datasocket = 0;
	}

	taskthread->cmdstatus = FTP_STATUS_WAITING;

	return DLD_ERR_OK;
}


INT ftp_taskthread_stop(DldTaskThread *dldtaskthread)
{

	FtpTaskThread *taskthread = (FtpTaskThread *)dldtaskthread;
	
	taskthread->status = DLD_TASKTHREAD_STOPPED;

	if(taskthread->fp)
	{
		fclose(taskthread->fp);
		taskthread->fp = NULL;
	}

	if(taskthread->connsocket)
	{
		os_socket_tcp_close(taskthread->connsocket);
		taskthread->connsocket = 0;
		dprintf("[ftp_taskthread_stop] close conn \r\n");
	}

	if(taskthread->datasocket)
	{
		os_socket_tcp_close(taskthread->datasocket);
		taskthread->datasocket = 0;
		dprintf("[ftp_taskthread_stop] close data \r\n");
	}

	taskthread->block = os_free(taskthread->block);

	return DLD_ERR_OK;
}


VOID ftp_taskthread_meet_error(FtpTaskThread *taskthread, BOOL replyerr)
{
	STRING *reply;
	CHAR *localstr;
	
	dld_taskthread_add_log((DldTaskThread *)taskthread, _("FTP connection meets an error."));
	if(replyerr)
	{
		if(taskthread->buffer->len >= 4)
		{
			reply = string_nappend(NULL, (char *)taskthread->buffer->data, taskthread->buffer->len);
			
			localstr = os_convert(reply->str);
			dld_taskthread_add_log((DldTaskThread *)taskthread, _("Server reply : \r\n%s\r\n"), localstr);
			localstr = os_convert_free(localstr);
			
			reply = string_free(reply);
		}
		
		os_socket_tcp_send(taskthread->connsocket, "ABOR\r\n", 6);
		os_socket_tcp_send(taskthread->connsocket, "QUIT\r\n", 6);
		os_sleep(100);
		
		ftp_taskthread_stop((DldTaskThread *)taskthread);  //stop it!
		
		if(taskthread->parenttask->filesize == 0 && dld_taskthread_is_first((DldTaskThread *)taskthread))
		{
			//Stop the task by ERROR!
			taskthread->parenttask->status = DLD_TASK_ERROR;
		}

	}
	if(!replyerr)
	{
		if(taskthread->status == FTP_STATUS_WELCOME)
		{
			dld_taskthread_add_log((DldTaskThread *)taskthread, _("Can not connect to remote host. Invalid Host or Too Many Connections.\n"));
		}
		else
		{
			dld_taskthread_add_log((DldTaskThread *)taskthread, _("Your network may not work correctly. Retry...\n"));
		}
		
		
		//retry...
		taskthread->retries ++;
		if(taskthread->retries <= g_config.maxretries)
		{
			os_tick_clear(taskthread->retrytick);
			ftp_taskthread_start((DldTaskThread *)taskthread);
		}
		else
		{
			ftp_taskthread_stop((DldTaskThread *)taskthread);
			dld_taskthread_add_log((DldTaskThread *)taskthread, _("Can not connect to remote host. Invalid Host or Too Many Connections.\n"));
		}	
	}
	
	if(dld_task_get_runningthreadnumber(taskthread->parenttask) == 0)
	{
		taskthread->parenttask->status = DLD_TASK_ERROR;
		dld_ui_callback(DLD_UICMD_TASK_STATUS, taskthread->parenttask);
	}	
}

/*
INT ftp_task_start(DldTask *task)
{
	task->status = DLD_TASK_RUNNING;
	//task->mainthread = os_thread_create(ftp_thread_main, task);
	return DLD_ERR_OK;
}


INT ftp_task_stop(DldTask *task)
{
	task->status = DLD_TASK_STOPPED;
	return DLD_ERR_OK;
}
*/

INT ftp_get_reply_code(FtpTaskThread *taskthread)
{
	if(taskthread->buffer)
		if(taskthread->buffer->len >= 4)
			if(taskthread->buffer->data[3] == ' ' || taskthread->buffer->data[3] == '-')
				return atoi(taskthread->buffer->data);

	return 1000;  //invalid code
}


BOOL ftp_reply_is_finished(FtpTaskThread *taskthread)
{
	CHAR rpend[10];
	CHAR *p;

	if(taskthread->buffer->len < 4) return FALSE;
	if( '0' <= taskthread->buffer->data[0] && taskthread->buffer->data[0] <= '9' &&
		'0' <= taskthread->buffer->data[1] && taskthread->buffer->data[1] <= '9' &&
		'0' <= taskthread->buffer->data[2] && taskthread->buffer->data[2] <= '9' &&
		(taskthread->buffer->data[3] == ' ' || taskthread->buffer->data[3] == '-') )
	{
		strncpy(rpend, taskthread->buffer->data, 3);
		rpend[3] = ' ';
		rpend[4] = 0;
		if((p = bufstr(taskthread->buffer, NULL, rpend)) != NULL)
			if(bufstr(taskthread->buffer, p, "\r\n"))
				return TRUE;

		return FALSE;
	}
	else
	{
		return TRUE; // invalid reply, it will raise an error
	}
}

INT ftp_recv_reply(FtpTaskThread *taskthread)
{
	CHAR buf[SOCKET_BUF_MAX + 1];
	BOOL readable, error;
	INT recvlen;

	os_socket_tcp_status(taskthread->connsocket, &readable, NULL, &error);
	if(readable)
	{
		recvlen = os_socket_tcp_recv(taskthread->connsocket, buf, SOCKET_BUF_MAX);
		//TODO
		// Why ???????????
		if(recvlen > 0)
		{
			taskthread->buffer = buffer_append(taskthread->buffer, buf, recvlen);
			
			//clear TIMEOUT tick
			os_tick_clear(taskthread->timeouttick);
			
			if(ftp_reply_is_finished(taskthread))
			{
				dprintf("[ftp_recv_reply] %d : %s\r\n", taskthread, taskthread->buffer->data);
				return ftp_get_reply_code(taskthread);
			}
			else
			{
				return -1;
			}
		}
		else
		{
			//zero
		}
	}
	
	if(error && !ftp_reply_is_finished(taskthread))
	{
		ftp_taskthread_meet_error(taskthread, FALSE);
	}
	
	return -1;
}

VOID ftp_connect(FtpTaskThread *taskthread)
{
	INT replycode;
	STRING *reply;
	CHAR *localstr;
	
	if(taskthread->cmdstatus == FTP_STATUS_WAITING)
	{
		if(taskthread->connsocket)
		{
			os_socket_tcp_close(taskthread->connsocket);
			taskthread->connsocket = 0;
		}

		if(taskthread->datasocket)
		{
			os_socket_tcp_close(taskthread->datasocket);
			taskthread->datasocket = 0;
		}
		
		if(taskthread->retries != 0)
		{
			//In retry mode ....
			//make a delay ...
			if(!os_tick_check(taskthread->retrytick))
				return;
		}		
		
		if(dld_taskthread_is_first((DldTaskThread *)taskthread))
		{
			dld_taskthread_add_log((DldTaskThread *)taskthread,_("Resolving host %s ..."), taskthread->parenttask->host->str);
			taskthread->parenttask->ip = string_free(taskthread->parenttask->ip);
			taskthread->parenttask->ip = os_socket_getip(taskthread->parenttask->host->str);
			
			os_tick_clear(taskthread->timeouttick);
		}		
		dld_taskthread_add_log((DldTaskThread *)taskthread, _("Connecting to %s:%s"), taskthread->parenttask->ip->str,taskthread->parenttask->port->str);
		taskthread->connsocket = os_socket_tcp_connect(taskthread->parenttask->ip->str, (USHORT)atoi(taskthread->parenttask->port->str), TRUE);
		
		if(taskthread->connsocket != 0)
		{
			taskthread->cmdstatus = FTP_STATUS_WELCOME;
			taskthread->buffer = buffer_clear(taskthread->buffer);
		}
		else
		{
			ftp_taskthread_meet_error(taskthread, FALSE);
		}
	}


	if(taskthread->cmdstatus == FTP_STATUS_WELCOME)
	{
		if((replycode = ftp_recv_reply(taskthread)) != -1)
		{
			if(replycode == 220)
			{
				reply = string_nappend(NULL, (char *)taskthread->buffer->data, taskthread->buffer->len);
				
				localstr = os_convert(reply->str);
				dld_taskthread_add_log((DldTaskThread *)taskthread, _("Server reply : \r\n%s\r\n"), localstr);
				localstr = os_convert_free(localstr);
				
				reply = string_free(reply);
				taskthread->cmdstatus = FTP_STATUS_WELCOME_OK;
				taskthread->buffer = buffer_clear(taskthread->buffer);
			}
			else
			{
				// connect ftp error!
				ftp_taskthread_meet_error(taskthread, FALSE);
			}
		}
	}
}


VOID ftp_close(FtpTaskThread *taskthread)
{
	dld_taskthread_add_log((DldTaskThread *)taskthread, _("Close connection."));

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -