From 7f95e4fb2c3d0a631a098a10b8051b7822c51a03 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Thu, 29 Jan 2015 16:30:47 -0600 Subject: [PATCH] Networking: Fix a major TCP bug introduced with commit e71c09ce9777ff732cb60bd07fb43d85522f79d6. Some connection logic was reorder -- setting the socket got moved BEFORE the point where the check was made if the socket was already connected. The resulting behavior was odd: Telnet would connect, but then when you exit and reconnect, it would fail to connect. But then if try again, it would connect okay. So the symptom was connect-fail-connect-fail-... --- net/socket/accept.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/net/socket/accept.c b/net/socket/accept.c index e73f772287d..3f8204b3669 100644 --- a/net/socket/accept.c +++ b/net/socket/accept.c @@ -185,7 +185,7 @@ int accept(int sockfd, FAR struct sockaddr *addr, FAR socklen_t *addrlen) if (addr) { - /* If an address is provided, then the lenght must also be provided. */ + /* If an address is provided, then the length must also be provided. */ DEBUGASSERT(addrlen); @@ -258,8 +258,6 @@ int accept(int sockfd, FAR struct sockaddr *addr, FAR socklen_t *addrlen) pnewsock->s_domain = psock->s_domain; pnewsock->s_type = SOCK_STREAM; - pnewsock->s_flags |= _SF_CONNECTED; - pnewsock->s_flags &= ~_SF_CLOSED; /* Perform the correct accept operation for this address domain */ @@ -306,6 +304,10 @@ int accept(int sockfd, FAR struct sockaddr *addr, FAR socklen_t *addrlen) } #endif /* CONFIG_NET_TCP */ + /* Mark the socket as connected. */ + + pnewsock->s_flags |= _SF_CONNECTED; + pnewsock->s_flags &= ~_SF_CLOSED; return newfd; errout_with_socket: