0
votes

I am trying to implement a basic ftp server.

I know that the data channel is created by the PASV command and that it creates a new socket that is used to send data like files.

I started to implement the command LIST, but I don't understand why should I close my data socket when the transfer finished. The ftp client that I am using for my test close his connection with my data socket when he receive the code 226.

That means that the client should do a PASV before every commands that require the data channel ?

My code looks like this at the moment :

int     list(char **arg, int c_sock, t_data *data)
{
  FILE      *fp;
  char      path[1035];

  write_to_client(c_sock, "150 ", "sending data...\r\n");
  fp = popen("/bin/ls /home/login -l", "r");
  if (fp == NULL)
    {
      printf("Failed to run command\n");
      exit(1);
    }
    while (fgets(path, sizeof(path)-1, fp) != NULL)
    {
      write_to_client(data->data_sock, path, "\0");
    }
  pclose(fp);
  write_to_client(c_sock, "226 ", "data send\r\n");
  close(data->data_sock);
  return (0);
}
1

1 Answers

1
votes

Short answer: you need to close the data socket because this is mandated by FTP protocol. FTP protocol says that the data transfer is finished when the socket is closed, and yes, new connection should be created for every data transfer.

It doesn't mean client has to repeat PASV (or PORT in active mode) command for every transfer, though - as long as the listening socket port remains valid, it can be reused.