Please help debug my socket code!

Eric Radman ericshane at eradman.com
Wed Feb 13 16:20:29 UTC 2019


On Wed, Feb 13, 2019 at 12:50:03AM -0600, Joe Nelson wrote:
> Here's some context about FTP. When a client connects, they interact
> with the FTP server through a "control channel." This is a TCP socket
> where the client can issue commands. However when the client wants to
> transfer files or get a directory listing they have to open a second
> "data" socket. Either the server determines the port number of this
> new socket, or the client does. This is called passive or active mode
> respectively. Once the port is chosen the server binds it and starts
> listening. The client connects to it, then issues the next command such
> as LIST on the control channel. The server then sends data through the
> data channel.

What does netstat(1) show?

> I've chosen to implement passive mode, which the client initiates with
> the PASV command. My server is able to pick a random available port and
> send the port value to the client. However when the client tries to
> connect it's as if the server isn't listening on that port. Wireshark
> shows me that the server responds with [RST ACK], refusing the client
> connection. This is super weird to me because I am calling listen() on
> the socket as part of my negotiate_listen() function, and I am choosing
> a TCP backlog of SOMAXCONN so there should be room in the connection
> queue for the client.
> 
> Here's the code, I'd love any help debugging.
> 
> https://github.com/begriffs/gitftp/blob/master/gitftp.c

Could this problem be caused by missing initialization of the addrinfo
struct?

diff --git a/gitftp.c b/gitftp.c
index 571a07f..58ab80c 100644
--- a/gitftp.c
+++ b/gitftp.c
@@ -55,6 +55,7 @@ int negotiate_listen(char *svc)
        int sock, e, reuseaddr=1;
        struct addrinfo hints = {0}, *addrs, *ap;

+       memset(&hints, 0, sizeof(hints));
        hints.ai_family   = AF_INET;  /* IPv4 required for PASV command */
        hints.ai_socktype = SOCK_STREAM;

--
  Eric


More information about the Friends mailing list