修改struct ftpuser结构体如下,将原来的connfd直接写到整个结构体中

struct ftpuser{
	char username[20];
	char userpass[20];
	char dataip[20];			//save ip for port mode
	char basedir[MAXLINE];			//basedir of current user
	int dataport;				//save port for port mode
	int loginok;				//whether user login
	int userok;				//user ok but no pass
	int transfor_mode;			//port or pasv
	int write_flag;				//write access control
	int maxspeed;				//speed limit
	int pasv_sockfd;			//pasv sockfd
	pthread_t pasv_accept;			//pasv listen thread id
        int connfd;                         //control sockfd
};
[/code]
pasv函数如下,当执行STOR,RETR,LIST后要关闭pasv_sockfd,特别是LIST用了一个子进程实现数据通信,父进程中也要关闭
[code=c]
void *pasv_listen(void *arg)		//create listen_thread for pasv data port
{
	struct ftpuser *myftp = (struct ftpuser *)arg;
	srand(time(NULL));
	int listenfd = -1;
	while(listenfd == -1)
	{
		myftp->dataport = rand()%45535 + 20000;			//generate a random port
		listenfd = initlisten(myftp->dataport);
	}
	char notifylisten[MAXLINE];
	memset(notifylisten,'\0',MAXLINE);
	snprintf(notifylisten,MAXLINE,"227 Entering Passive Mode (202,117,7,222,%d,%d)\r\n",(myftp->dataport)/256,(myftp->dataport)%256);
	write(myftp->connfd,notifylisten,strlen(notifylisten));
	int connfd = accept(listenfd,NULL,NULL);
	pthread_mutex_lock(&lock);
	myftp->pasv_sockfd = connfd;
	pthread_mutex_unlock(&lock);
	myftp->transfor_mode = 2;
	return;
}

void do_pasv(int connfd,struct ftpuser *myftp)
{
	cleanmode(myftp);
	//char *notsupport="500 passive mode not supported.\r\n";
	if(myftp->pasv_accept != 0)
		pthread_cancel(myftp->pasv_accept);					//ingore the error while the tid is not exist
	pthread_t tid;
	pthread_create(&tid,NULL,pasv_listen,(void *)myftp);
	myftp->pasv_accept = tid;
	return;
}
转载请注明来源:Leoncom-《FTPServer的PASV模式》
Trackback

only 1 comment untill now

  1. 外日

Add your comment now