实施ftp服务器“ LIST”的问题

问题描述

我正在实现FTP服务器,在这里将winscp作为客户端。每当客户端向我(服务器)发送PASV命令时,我都会向客户端发送具有IP地址和端口的回复,然后客户端正在发送LIST命令,然后我给出响应作为客户

150 opening ASCII mode data connection for file list
-rwxrwxr-x 1 elmpc-162 elmpc-162 17608 Sep 16 12:13 a.out
226 File transfer completed...

这些回复是我在第二个端口发送的。

像这样,但是winscp始终显示“ 8无法检索目录列表**”

但是如果我在winscp日志中看到的数据不存在,那么我发送给客户端的数据将不存在

您可以看到日志。...

. 2020-09-17 16:02:54.408 Username prompt (no username provided)
. 2020-09-17 16:03:00.663 Connecting to 192.168.5.37:5005 ...
. 2020-09-17 16:03:00.663 Connected with 192.168.5.37:5005. Waiting for welcome message...
< 2020-09-17 16:03:00.663 220 Welcome to Alan's FTP site
> 2020-09-17 16:03:00.663 USER srihari
< 2020-09-17 16:03:00.663 331 Password required 
> 2020-09-17 16:03:04.064 PASS *****
< 2020-09-17 16:03:04.064 230 Public login sucessful 
> 2020-09-17 16:03:04.064 SYST
< 2020-09-17 16:03:04.064 215 AmigaOS 
> 2020-09-17 16:03:04.064 FEAT
< 2020-09-17 16:03:04.064 202 Command not implemented,superfluous at this site. 
. 2020-09-17 16:03:04.079 Connected
. 2020-09-17 16:03:04.079 --------------------------------------------------------------------------
. 2020-09-17 16:03:04.079 Using FTP protocol.
. 2020-09-17 16:03:04.079 Doing startup conversation with host.
> 2020-09-17 16:03:04.095 PWD
< 2020-09-17 16:03:04.095 257 "/home/elmpc-162/ftp_server" 
. 2020-09-17 16:03:04.095 Getting current directory name.
. 2020-09-17 16:03:04.142 Retrieving directory listing...
> 2020-09-17 16:03:04.142 TYPE A
< 2020-09-17 16:03:04.142 200 Switching to ASCII mode. 
> 2020-09-17 16:03:04.142 PASV
< 2020-09-17 16:03:04.142 227 Entering Passive Mode (192,168,5,37,23,112) 
> 2020-09-17 16:03:04.142 LIST -a
. 2020-09-17 16:03:04.142 Connecting to 192.168.5.37:6000 ...
. 2020-09-17 16:03:19.757 Timeout detected. (data connection)

但是在服务器中,这表明客户端已连接。

在这里我无法找出原因,为什么对LIST的回应没有了

任何人都可以帮助我

这是我的代码

#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> //Header file for sleep(). man 3 sleep for details. 
#include <pthread.h> 
#include <stdio.h> 
#include <netdb.h> 
#include <netinet/in.h> 
#include <stdlib.h> 
#include <string.h> 
#include <sys/socket.h> 
#include <sys/types.h> 
#define MAX 80 
#define PORT1 5013
#define PORT2 6004
#define SA struct sockaddr 
#define SA struct sockaddr 

char read_buffer[200],write_buffer[200];
char check=0;
void call_second(void);
// A normal C function that is executed as a thread 
// when its name is specified in pthread_create() 
void *first_thread(void *vargp) 
{ 
    printf("in first thread.....\n");
    int sockfd,connfd,len; 
    struct sockaddr_in servaddr,cli; 

    // socket create and verification 
    sockfd = socket(AF_INET,SOCK_STREAM,0); 
    if (sockfd == -1) { 
        printf("socket creation Failed...\n"); 
        exit(0); 
    } 
    else
        printf("Socket successfully created..\n"); 
    bzero(&servaddr,sizeof(servaddr)); 

    // assign IP,PORT 
    servaddr.sin_family = AF_INET; 
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY); 
    servaddr.sin_port = htons(PORT1); 

    // Binding newly created socket to given IP and verification 
    if ((bind(sockfd,(SA*)&servaddr,sizeof(servaddr))) != 0) { 
        printf("socket bind Failed...\n"); 
        exit(0); 
    } 
    else
        printf("Socket successfully binded..\n"); 

    // Now server is ready to listen and verification 
    if ((listen(sockfd,5)) != 0) { 
        printf("Listen Failed...\n"); 
        exit(0); 
    } 
    else
        printf("Server listening..\n"); 
    len = sizeof(cli); 

    // Accept the data packet from client and verification 
    connfd = accept(sockfd,(SA*)&cli,&len); 
    if (connfd < 0) { 
        printf("server acccept Failed...\n"); 
        exit(0); 
    } 
    else
        printf("server acccept the client...\n"); 

    //bzero(buffer,0); 
        // sprintf(buffer,"\n220 Welcome to Alan's FTP site \r\n\n");
        
        //for (;;) 
        //write(sockfd,"welcome",sizeof("welcome")); 
    // Function for chatting between client and server 
    //printf("after writing into sockbuffer ...\n");
    //func(connfd); 

    int n,bytes; 
    // infinite loop for chat 
    for (;;) 
    { 
        
        //bzero(buff,MAX); 
        memset(read_buffer,200);
        memset(write_buffer,200);
        sprintf(write_buffer,"\n220 Welcome to Alan's FTP site\r\n\n");
         write(connfd,write_buffer,sizeof(write_buffer)); 
        printf("come inside..\n");
        // read the message from client and copy it in buffer 
        while(1)
        {
        //sleep(1);
        memset(read_buffer,200);
        //printf("before reading....\n");
        bytes=read(connfd,read_buffer,sizeof(read_buffer)); 
        //printf("checking blocking or not..  %s\n",read_buffer);
        if(bytes>0)
        {
        printf("From client:%s%d\n",bytes);
        } 
        // if(strstr(buff,"AUTH TLS")||strstr(buff,"AUTH SSL"))
        // {
        //  sprintf(buffer,"\n502 Command not implemented\r\n\n");
        //  write(sockfd,buffer,sizeof(buffer)); 
        // }
        if (strstr(read_buffer,"USER"))
            {
                printf("Logging in \n");
                sprintf(write_buffer,"331 Password required \r\n");
                bytes = write(connfd,sizeof(write_buffer)); 
                if (bytes < 0) break;
            }
        if (strncmp(read_buffer,"PASS",4)==0)
            {
                printf("Typing password (anything will do... \n");
                sprintf(write_buffer,"230 Public login sucessful \r\n");
                bytes = write(connfd,"SYST",4)==0)
            {
                printf("215 AmigaOS \n");
                //sprintf(write_buffer,"215 AmigaOS \r\n");
                sprintf(write_buffer,"215 UNIX emulated by FileZilla \r\n");
                bytes = write(connfd,sizeof(write_buffer));
                memset(write_buffer,sizeof(write_buffer));
                
            }
        if (strncmp(read_buffer,"CLNT",4)==0)
            {
                printf("200 Don't care \n");
                sprintf(write_buffer,"200 Don't care \r\n");
                bytes = write(connfd,"OPTS UTF8 ON",4)==0)
            {
                printf("202 UTF8 mode is always enabled. No need to send this command. \n");
                sprintf(write_buffer,"202 UTF8 mode is always enabled. No need to send this command. \r\n");
                bytes = write(connfd,"FEAT",4)==0)
            {
               printf("211-Features:  \n");
                sprintf(write_buffer,"211-Features: \r\n");
                bytes = write(connfd,sizeof(write_buffer)); 
                sprintf(write_buffer,"MDTM \r\n");
                bytes = write(connfd,sizeof(write_buffer)); 
                memset(write_buffer,sizeof(write_buffer));
                sprintf(write_buffer,"REST STREAM \r\n");
                bytes = write(connfd,"MLST type*;size*;modify*; \r\n");
                bytes = write(connfd,sizeof(write_buffer));  
                // memset(write_buffer,sizeof(write_buffer));
                // sprintf(write_buffer,"MLSD \r\n");
                // bytes = write(connfd,sizeof(write_buffer));   
                memset(write_buffer,"UTF8 \r\n");
                bytes = write(connfd,sizeof(write_buffer));  
                memset(write_buffer,"CLNT \r\n");
                bytes = write(connfd,"MFMT \r\n");
                bytes = write(connfd,"EPSV \r\n");
                bytes = write(connfd,"EPRT \r\n");
                bytes = write(connfd,"211 End \r\n");
                bytes = write(connfd,sizeof(write_buffer));
                printf("211 end:  \n");
               
            }
        if (strncmp(read_buffer,"PWD",3)==0)
            {
                printf("present working directory \n");
                //  FILE *fpipe;
                 char pwd[100],i=5;
                // char *command = "pwd";
                // char c = 0;

                // if (0 == (fpipe = (FILE*)popen(command,"r")))
                // {
                //  perror("popen() Failed.");
                //  exit(EXIT_FAILURE);
                // }
                // strcat(pwd,"257 \"");
                // printf("%s\n",pwd);
                // while (fread(&c,sizeof c,1,fpipe))
                // {
                //  //printf("%c",c);
                //  pwd[i++]=c;

                // }
                // strcat(pwd,"\" \r\n");
                memset(pwd,sizeof(pwd));
                // strcat(pwd,"257 \"/\"\r\n");
                strcat(pwd,"257 \"/home/elmpc-162/ftp_server\" ");
                printf("%s\n",pwd);

                //pclose(fpipe);
                // sprintf(write_buffer,"202 Command not implemented,superfluous at this site. \r\n");
                bytes = write(connfd,pwd,sizeof(pwd)); 
                if (bytes < 0) break;
            }
        if (strstr(read_buffer,"TYPE I"))
            {
                printf(" 200 Switching to Binary mode.\n");
                sprintf(write_buffer,"200 Switching to Binary mode. \r\n");
                bytes = write(connfd,sizeof(write_buffer)); 
                if (bytes < 0) break;
            }
        if (strstr(read_buffer,"TYPE A"))
            {
                printf(" 200 Switching to ASCII mode.\n");
                sprintf(write_buffer,"200 Switching to ASCII mode. \r\n");
                bytes = write(connfd,"AUTH TLS"))
            {
                printf(" asking AUTHTLS\n");
                sprintf(write_buffer,"502 Explicit TLS authentication not allowed \r\n");
                bytes = write(connfd,"AUTH SSL"))
            {
                printf(" asking AUTHTLS\n");
                sprintf(write_buffer,"CDUP"))
            {
                printf(" got CDUP command \n");
                sprintf(write_buffer,"457 \"/home\" \r\n");
                bytes = write(connfd,"CWD"))
            {
                printf(" got CWD command \n");
                sprintf(write_buffer,"457 \"/home/elmpc-162/ftp_server\" \r\n");
                bytes = write(connfd,"PASV",4)==0)
            {int a,b;
                //close(sockfd);
                a=PORT2/256;
                b=PORT2%256;
                printf("Entering into passvie mode \n");
                sprintf(write_buffer,"227 Entering Passive Mode (192,%d,%d) \r\n",a,b);
                bytes = write(connfd,sizeof(write_buffer)); 
                if (bytes < 0) break; 
                // check =1;
                   //exit(0);             
            }
        if (strstr(read_buffer,"LIST")||strstr(read_buffer,"MLSD"))
            {
sprintf(write_buffer,"150 Transfering... \r\n");
                //printf("%s\n",write_buffer);
                bytes = write(connfd,sizeof(write_buffer)); 
                if (bytes < 0) break;


                memset(write_buffer,sizeof(write_buffer));
                //strcat(pwd,"-rwxrwxr-x 1 elmpc-162 elmpc-162 17608 Sep 16 12:13 a.out \r\n");
                // sprintf(write_buffer,"type=file;modify=20200902093315;size=302; a.cfg \r\n");
                // sprintf(write_buffer,"a.out \r\n");
                // sprintf(write_buffer,"%s",pwd);
                // write(connfd,sizeof(write_buffer)); 
                call_second();

                memset(write_buffer,"226 File transfer completed... \r\n");
                //printf("%s\n",write_buffer);
                write(connfd,sizeof(write_buffer)); 
                 printf("wrote==%d\n",n);
                memset(write_buffer,sizeof(write_buffer));


                check =1;
            }

        // // print buffer which contains the client contents 
        
        }
        
        n = 0; 
        
    
    }

    return NULL; 
} 
void *second_thread(void *vargp) 
{ 
    while(1)
    {
        //printf("dfjhdkfhdf\n");
        // if(check==1)
        // call_second();
    }
}
void call_second(void)
{
    int sockfd_c,connfd_c,len_c; 
    struct sockaddr_in servaddr_c,cli_c; 

    // socket create and verification 
    sockfd_c = socket(AF_INET,0); 
    if (sockfd_c == -1) { 
        printf("socket creation Failed...2\n"); 
        exit(0); 
    } 
    else
        printf("Socket successfully created..2\n"); 
    bzero(&servaddr_c,sizeof(servaddr_c)); 

    // assign IP,PORT 
    servaddr_c.sin_family = AF_INET; 
    servaddr_c.sin_addr.s_addr = htonl(INADDR_ANY); 
    servaddr_c.sin_port = htons(PORT2); 

    // if (connect(sockfd_c,(SA*)&servaddr_c,sizeof(servaddr_c)) != 0) { 
    //  printf("connection with the server Failed...\n"); 
    //  exit(0); 
    // } 
    // else
    //  printf("connected to the server..\n"); 

    // Binding newly created socket to given IP and verification 
    if ((bind(sockfd_c,sizeof(servaddr_c))) != 0) { 
        printf("socket bind Failed...2\n"); 
        exit(0); 
    } 
    else
        printf("Socket successfully binded..2\n"); 

    // Now server is ready to listen and verification 
    if ((listen(sockfd_c,5)) != 0) { 
        printf("Listen Failed...2\n"); 
        exit(0); 
    } 
    else
        printf("Server listening..2\n"); 
    len_c = sizeof(cli_c); 

    // Accept the data packet from client and verification 
    connfd_c = accept(sockfd_c,(SA*)&cli_c,&len_c); 
    if (connfd_c < 0) { 
        printf("server acccept Failed...2\n"); 
        exit(0); 
    } 
    else
        printf("server acccept the client...2\n"); 

    
    char buff[MAX]; 
    int n; 
    // infinite loop for chat 
    //while(1)
    {
     if(check==1)
    {
    
                
                check =1;

                FILE *fpipe;
                int k=0;
                 char pwd[1500];int i=0;

                char *command = "ls -l";
                char c = 0;
                memset(pwd,sizeof(pwd));
                 
                
                if (0 == (fpipe = (FILE*)popen(command,"r")))
                {
                    perror("popen() Failed.");
                    exit(EXIT_FAILURE);
                }
                
                while (fread(&c,fpipe))
                {
                    //printf("%c",c);
                    k++;
                    if(k>9)
                    pwd[i++]=c;

                }
                // printf("pwd==%s\n",pwd);
                k=0;

                                
                memset(write_buffer,sizeof(write_buffer));
                // strcat(pwd,"-rwxrwxr-x 1 elmpc-162 elmpc-162 17608 Sep 16 12:13 a.out1 \r\n");
                // strcat(pwd,"-rwxrwxr-x 1 elmpc-162 elmpc-162 17608 Sep 16 12:14 a.out2 \r\n");
                // strcat(pwd,"-rwxrwxr-x 1 elmpc-162 elmpc-162 17608 Sep 16 12:15 a.out3 \r\n");
                sprintf(write_buffer,pwd);
                write(connfd_c,sizeof(write_buffer)); 
                
                sleep(3);
                
     check=0; 
 
    }
    }
    // After chatting close the socket 
    close(sockfd_c);
    printf("closed second connection...\n"); 
}
    


int main() 
{ 
    pthread_t thread_id1,thread_id2; 
    printf("Before Thread\n"); 
    pthread_create(&thread_id1,NULL,first_thread,(void *)&thread_id1); 
    //pthread_join(thread_id1,NULL); 
    pthread_create(&thread_id2,second_thread,(void *)&thread_id2); 
    //pthread_join(thread_id2,NULL);
    printf("After Thread\n"); 
    pthread_exit(NULL); 
    //exit(0); 
}

解决方法

. 2020-09-17 16:03:04.142 Connecting to 192.168.5.37:6000 ...
. 2020-09-17 16:03:19.757 Timeout detected. (data connection)

客户端无法建立与服务器的数据连接。鉴于错误是 Timeout 而不是拒绝连接,我怀疑客户端和服务器之间(或服务器上)之间存在防火墙阻止了访问(丢弃)交通)。但是,由于您的问题(如代码和服务器设置)中没有可重现的内容,所以这只是有根据的猜测。

,

大多数FTP服务器都有一个称为IP伪装的设置,如果FTP服务器位于NAT之后,则必须将其配置为公共IP地址。

此响应“ 227 Entering Passive Mode(192,168,5,37,23,112)”应发送公用IP地址,并且必须在服务器端配置端口转发以建立连接