Pergunta

I have to code a FTP server and I have a problem on how data is transferred.

I'm using ftp command on linux to test it and I'm currently working on LIST, which sends the list of files/directories in the working directory. Everything works fine (ftp well prints the list of files) except that ftp prints the following warning:

WARNING! 5 bare linefeeds received in ASCII mode
File may not have transferred correctly.

I would like to remove that warning, I think ftp is expecting binary data but I don't know how to send data like that through socket, I'm currently doing the most basic way to send ls result:

dup2(dst_channel, 1);
execlp("ls", "ls", "-l", NULL);
Foi útil?

Solução

The FTP protocol specifies that the LIST command should send its result in NETASCII, which means that newlines are represented as CRLF. But standard Linux programs output just LF for newlines.

You need to run the ls command with its output connected to a pipe. Your server reads from the pipe, replaces any \n characters with `\r\n', and writes this to the socket.

You'll need to do something similar when transferring actual files, except when the client requests binary mode.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top