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

📄 myftp.c

📁 LINUX下开发的FTP服务器,基本实现FTP的功能.实现PORT的模式.
💻 C
字号:
#include <unistd.h> 
#include <signal.h> 
#include <sys/param.h> 
#include <sys/types.h> 
#include <sys/stat.h> 
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include "commond_action.h"
#include "str.h"
char commond_buffer[100]={0};
char commond[50]={0};
char valus[50]={0};
char pwd[500]={0};


int timeout_check(int clientfd)
{
	fd_set rfds;
	struct timeval tv;
	int return_val;
	tv.tv_sec=120;
	tv.tv_usec=0;
	FD_ZERO(&rfds);
	FD_SET(clientfd,&rfds);
	return_val=select(clientfd+1,&rfds,NULL,NULL,&tv);
	return return_val;
}

void signal_handle(int signnum)
{
	wait(0);
}

void daemon_threads()
{
	pid_t		ch;
	int i=0;
	ch=fork();
	if(ch == 0)
	{
		setsid();
		ch=fork();
		if(ch == 0)
		{
			chdir("/tmp");
			umask(0);
		}else if(ch >0)
		{
			exit(0);
		}
	}
	else if(ch >0)
	{
		exit(0);
	}
			
}

int strat_ftp()
{	
	struct sockaddr_in ftp_s;
	int socket_s;
	int read_count=0;
	int addr_len=sizeof(struct sockaddr_in);
	int i=0;
	pid_t ch;

	int opval=1;
	int return_val;

	socket_s=socket(AF_INET,SOCK_STREAM,0);
	signal(SIGCHLD,signal_handle);
	signal(SIGQUIT,signal_handle);
	if(socket_s<0)
	{
		perror("create error");
		return FAIL;
	}

	ftp_s.sin_family=AF_INET;
	ftp_s.sin_port=htons(5555);
	ftp_s.sin_addr.s_addr=INADDR_ANY;

	setsockopt(socket_s,SOL_SOCKET,SO_REUSEADDR,&opval,sizeof(opval));

	if(bind(socket_s,(struct sockaddr*)&ftp_s,addr_len)<0)
	{
		perror("bind error");
		return FAIL;
	}
	if (listen(socket_s,13)<0)
	{
		perror("listen error");
		return FAIL;
	}
	while (1)
	{
		client_infor.socket_control=accept(socket_s,NULL,NULL);
		if (client_infor.socket_control<0)
		{
			perror("accept");
			return FAIL;
		}
		memcpy(commond_buffer,"220 Serv-U FTP Server v1.1\r\n",strlen("220 Serv-U FTP Server v1.1\r\n"));
		write(client_infor.socket_control,commond_buffer,strlen(commond_buffer));
		
		ch=fork();
		if (ch == 0)
		{
			close(socket_s);
			while (1)
			{
				return_val=timeout_check(client_infor.socket_control);
				if (return_val == 0)
				{
					break;
				}
				else if (return_val < 0)
				{
					if (errno==EINTR)
					{
						continue;
					}
					else
					{
						perror("select return");
						break;
					}
				}
				else
				{
					memset(commond_buffer,0,100);
					read_count=read(client_infor.socket_control,commond_buffer,100);
					if (read_count<=0)
					{
						break;
					}
					printf("read_commond_buffer:%s",commond_buffer);/*测试代码*/
					apart_comm_valus(commond_buffer,commond,valus);
					printf("commond:%s\n",commond);/*测试代码*/
					printf("valus:%s\n",valus);/*测试代码*/
					while (commond_list[i].commond)
					{
						if (strcmp(commond_list[i].commond,commond) == 0)
						{
							commond_list[i].action(valus);
							if (strcmp(valus ,"I")== 0)
							{
								printf("执行到此!\n");
							}
							write(client_infor.socket_control,commond_buffer,strlen(commond_buffer));
							if (strcmp(valus ,"I")== 0)
							{
								printf("执行到Write!\n");
							}
							printf("send_commond_buffer:%s",commond_buffer);/*测试代码*/
							break;
						}
						i++;
					}
					if (strcmp(commond,"QUIT") == 0)
					{
						close(client_infor.socket_control);
						break;
					}
					i=0;
				}
			}			
			exit(0);
		}
		close(client_infor.socket_control);
	}
}

void stop_ftp(char *exec_name)
{
	pid_t pid,ch;
	char temp[500];
	char tmp[10];
	char cmd[50];
	int f_sign[2];
	char *position=NULL;
	char *end_str=NULL;
	memset(temp,0,500);
	
	pipe(f_sign);
	ch=fork();
	if(ch == 0)
	{
		close(f_sign[0]);
		close(1);
		dup(f_sign[1]);
		execlp("pidof","pidof",exec_name,0);
		exit(0);
	}
	close(f_sign[1]);
	read(f_sign[0],temp,500);
	position=temp;
	do
	{
		end_str=find_first_of(position,' ');
		if (end_str != NULL)
		{
			memset(tmp,0,10);
			memset(cmd,0,10);
			copy_str(temp,tmp,end_str-position,position-temp);
			position=end_str+1;
			if(getpid() == atoi(tmp))
			{
				continue;
			}
			sprintf(cmd,"kill -9 %d",atoi(tmp));
			system(cmd);
		}
		else
		{
			end_str=find_first_of(position,'\n');
			if (end_str != NULL)
			{
				memset(tmp,0,10);
				memset(cmd,0,10);
				copy_str(temp,tmp,end_str-position,position-temp);
				position=end_str+1;
				if(getpid() == atoi(tmp))
				{
					continue;
				}
				sprintf(cmd,"kill -9 %d",atoi(tmp));
				system(cmd);
			}
			else
			{
				break;
			}
		}
	}
	while (1);
}


int test_is_start(char *exec_name)
{
	pid_t ch;
	int f_id[2];
	char buffer[100];
	memset(buffer,0,100);
	char *position=NULL;
	pipe(f_id);
	ch=fork();
	if(ch == 0)
	{
		close(f_id[0]);
		close(1);
		dup(f_id[1]);
		close(f_id[1]);
		execlp("pidof","pidof",exec_name,0);
		exit(0);
	}
	close(f_id[1]);
	read(f_id[0],buffer,100);
	position=find_first_of(buffer,' ');
	if(position != NULL)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}
void  print_infor(char *str)
{
	int i=1;
	printf("%s",str);
		while(i++<=30)
		{
			usleep(10000);
			printf(".");
		}
		printf("OK\n");
}

int main(int argc, char *argv[])
{
	if(strcmp(argv[1],"start") == 0)
	{
		if(test_is_start(argv[0]))
		{
			printf("FTP is running.\n");
		}
		else
		{
			print_infor("FTP start");
		//	daemon_threads();
			strat_ftp();
		}
	}
	else if(strcmp(argv[1],"restart") == 0)
	{
		if(test_is_start(argv[0]))
		{
			stop_ftp(argv[0]);
			print_infor("FTP stop");
			print_infor("FTP start");
			strat_ftp();
		}
		else
		{
			print_infor("FTP is not running.\n");
		}
	}
	else if(strcmp(argv[1],"stop") == 0)
	{
		if(test_is_start(argv[0]))
		{
				stop_ftp(argv[0]);
				print_infor("FTP stop");
		}
		else
		{
			printf("FTP is not running.\n");
		}
	}
	else
	{
		printf("commond error!\n");
	}
	return 0;
}

⌨️ 快捷键说明

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