#include "mydefine.h"
#include "com.h"
#include "pub.h"
#include "net.h"
char oldname[20];//do_rnfr
int data_conn_sock;
char connuser[10];
char up_com[20];
char port_ip[50] ;//PORT命令处理完后记IP用的
int port_port ;// PORT 命令处理完后记port用的
int restflag;//记录do_rest
int pasv_cannsock;
void get_com(char buf[],char com[])
{
char temp[1024];
strcpy(temp,buf);
char *p=strchr(temp,' ');
if(p!=NULL)
{
*p='\0';
strcpy(com,temp);
}
else
{
char *p=strchr(temp,'\r');
if(p!=NULL)
{
*p='\0';
strcpy(com,temp);
}
}
}
void get_charm(char buf[],char charm[])
{
char tmp[1024];
strcpy(tmp,buf);
char *p =strchr(tmp,' ');
if ( p != NULL)
{
strcpy(charm,p+1);
p = strchr(charm,'\r');
if( p != NULL )
{
*p='\0';
}
}
}
void do_user(SESSION nowsession)
{
strcpy(connuser,nowsession.charm);//保存用户名
if(strcmp(connuser,"anonumous")==0)
{
strcpy(connuser,"ftp");
}
write_loop(connfd,"331 give me pass\r\n",strlen("331 give me pass\r\n"));
}
void do_pass(SESSION nowsession)
{
if(strcmp(connuser,"ftp")!=0)
{
int res=checkpass(connuser,nowsession.charm);
if(res!=0)//密码错
{
write_loop(connfd,"530 pass is error\r\n",strlen("530 pass is error\r\n"));
return;
}
}
write_loop(connfd,"230 Login successful\r\n",strlen("230 Login successful\r\n"));
//切换有效身份
egid_euid(connuser,0);
}
void do_syst(SESSION nowsession)
{
write_loop(connfd,"215 Linux\r\n",strlen("215 Linux\r\n"));
}
void do_pwd(SESSION nowsession)
{
char sdir[200]="";
sprintf(sdir,"257 \"%s\"\r\n",nowsession.dir);
write_loop(connfd,sdir,strlen(sdir));
}
void do_type(SESSION nowsession)
{
if(strcmp(nowsession.charm,"A")==0)
{
write_loop(connfd,"200 Switching to ASCII mode\r\n",strlen("200 Switching to ASCII mode\r\n"));
}
else
write_loop(connfd,"200 I\r\n",strlen("200 I\r\n"));
}
void do_pasv(SESSION nowsession)
{
struct sockaddr_in servaddr;
struct sockaddr_in tempaddr;
unsigned int port1,port2,pasv_port;
char pasv_ip[]="192.168.50.10";
char *p=pasv_ip;
while(*p != '\0')
{
if( *p == '.')
*p=',';
p++;
}
//1
pasv_cannsock = socket(PF_INET,SOCK_STREAM,0);
if(pasv_cannsock == -1)
ERR_EXIT("pasv socket");
//2
memset(&servaddr,0,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(0);
int opt = 1;
int ret = setsockopt(pasv_cannsock,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));
if (ret == -1)
ERR_EXIT("setsockopt");
int res = bind(pasv_cannsock,(struct sockaddr *)&pasv_cannsock,sizeof(servaddr));
//if (res == -1)
//ERR_EXIT("pasv bind");
//3
res=listen(pasv_cannsock,10);
if (res == -1)
ERR_EXIT("pasv listen ");
//4
memset(&tempaddr,0,sizeof(tempaddr));
socklen_t addrlen=sizeof(tempaddr);
res = getsockname(pasv_cannsock,(struct sockaddr*)&tempaddr,(socklen_t *)&addrlen);
if (res == -1)
ERR_EXIT("pasv getsockname");
// printf("myip=%s port=%d\n",inet_ntoa(tempaddr.sin_addr),ntohs(tempaddr.sin_port));
pasv_port =ntohs(tempaddr.sin_port);
port1=pasv_port>>8;
port2=pasv_port&0xFF;
char buf[200];
memset(buf,0,sizeof(buf));
sprintf(buf,"227 Entering Passive Mode (%s,%d,%d)\r\n",pasv_ip,port1,port2);
write_loop(connfd,buf,strlen(buf));
strcpy(up_com,"PASV");
//printf("sssssssssssss\n");
}
void do_rest(SESSION nowsession)
{
restflag=atoi(nowsession.charm);
write_loop(connfd,"350 rest ok\r\n",strlen("350 rest ok\r\n"));
}
void do_stor(SESSION nowsession)
{
int fd;
if(restflag>0)
{
fd = open(nowsession.charm,O_WRONLY);
if(fd == -1)
{
printf(" stor open fail not directary!! ");
return;
}
lseek(fd,restflag,SEEK_SET);
restflag=0;
}
else
{
fd = open(nowsession.charm,O_CREAT|O_WRONLY,0666);
if(fd == -1)
{
printf(" stor open fail not directary!! ");
return;
}
}
data_conn_sock = test_port_pasv();
char buf[1024];
write_loop(connfd,"150 OK send to data\r\n",strlen("150 OK send to data\r\n"));
while(1)
{
memset(buf,0,sizeof(buf));
int n=read(data_conn_sock,buf,1024);
if(n<=0)
break;
write(fd,buf,n);
}
//write_loop(data_conn_sock,nowsession.charm,strlen(nowsession.charm));
write_loop(connfd,"226 File receive ok\r\n",strlen("226 File receive ok\r\n"));
if(strcmp(up_com,"PASV") == 0)
{
close (pasv_cannsock);
}
close (data_conn_sock);
}
void do_retr(SESSION nowsession)
{
int fd;
if(restflag>0)
{
fd = open(nowsession.charm,O_RDONLY);
if(fd == -1)
{
printf(" stor open fail not directary!! ");
return;
}
lseek(fd,restflag,SEEK_SET);
restflag=0;
}
else
{
fd = open(nowsession.charm,O_RDONLY,0666);
if(fd == -1)
{
printf(" stor open fail not directary!! ");
return;
}
}
data_conn_sock = test_port_pasv();
char buf[1024];
write_loop(connfd,"150 Opening BINARY mode data connection for myfle.txt\r\n",strlen("150 Opening BINARY mode data connection for myfle.txt\r\n"));
while(1)
{
memset(buf,0,sizeof(buf));
int n=read(fd,buf,1024);
if(n<=0)
break;
write(data_conn_sock,buf,n);
}
write_loop(connfd,"226 File send ok\r\n",strlen("226 File send ok\r\n"));
if(strcmp(up_com,"PASV") == 0)
{
close (pasv_cannsock);
}
close (data_conn_sock);
}
void do_dele(SESSION nowsession)
{
int res = remove(nowsession.charm);//inlink
write_loop(connfd,"250 Delete operation successful.\r\n",strlen("250 Delete operation successful.\r\n"));
}
void do_cwd(SESSION nowsession)
{
chdir(nowsession.charm);
write_loop(connfd,"250 directory successfully changed\r\n",strlen("250 directory successfully changed\r\n"));
}
void do_mkd(SESSION nowsession)
{
mkdir(nowsession.charm,0777);
write_loop(connfd,"257 \r\n",strlen("257 \r\n"));
}
void do_rmd(SESSION nowsession)
{
rmdir(nowsession.charm);
write_loop(connfd,"250 Remove directory successfully change\r\n",strlen("250 Remove directory successfully change\r\n"));
}
void do_port(SESSION nowsession)
{
//printf("do_port is unnunnnn\n");
write_loop(connfd,"200 PORT is ok\r\n",strlen("200 PORT is ok\r\n"));
char tmp[30];
strcpy(tmp,nowsession.charm);
int n1,n2,n3,n4,n5,n6;
sscanf(tmp,"%d,%d,%d,%d,%d,%d",&n1,&n2,&n3,&n4,&n5,&n6);
sprintf(port_ip,"%d.%d.%d.%d",n1,n2,n3,n4);
port_port = n5*256+n6;
strcpy(up_com,"PORT");
}
void do_list(SESSION nowsession)
{
data_conn_sock = test_port_pasv();
write_loop(connfd,"150 Start directory send\r\n",strlen("150 Start directory send\r\n"));
mylist(data_conn_sock,nowsession.dir);//connuser
write_loop(connfd,"226 Directory send ok\r\n",strlen("226 Directory send ok\r\n"));
if(strcmp(up_com,"PASV") == 0)
{
close (pasv_cannsock);
}
close (data_conn_sock);
//printf("do_list() is running!\n");
}
void do_cdup(SESSION nowsession)
{
chdir("../");
write_loop(connfd,"250 directory successfully change\r\n",strlen("250 directory successfully change\r\n"));
}
void do_site(SESSION nowsession)
{
char tmp[30];
char md[20];
char fil_name[20];
int n,m;
strcpy(tmp,nowsession.charm);
sscanf(tmp,"%s %d %s",md,&n,fil_name);
m=n%10+n/10%10*8+n/100*64;
chmod(fil_name,(mode_t)m);//
write_loop(connfd,"200 site mode success.\r\n",strlen("200 site mode success.\r\n"));
}
void do_rnfr(SESSION nowsession)
{
strcpy(oldname,nowsession.charm);
write_loop(connfd,"350 read for RETO\r\n",strlen("350 read for RETO\r\n"));
}
void do_rnto(SESSION nowsession)
{
rename(oldname, nowsession.charm);
write_loop(connfd,"250 rename ok\r\n",strlen("250 rename ok\r\n"));
}
void do_quit(SESSION nowsession)
{
write_loop(connfd,"221 Goodbye\r\n",strlen("221 Goodbye\r\n"));
}
COM_FUN mycomfun[]={
{"USER", do_user},
{"PAS