diff --git a/net/tcp/tcp_send_buffered.c b/net/tcp/tcp_send_buffered.c index b4102ba2f23..389cc8a4741 100644 --- a/net/tcp/tcp_send_buffered.c +++ b/net/tcp/tcp_send_buffered.c @@ -1033,14 +1033,15 @@ ssize_t psock_tcp_send(FAR struct socket *psock, FAR const void *buf, bool nonblock; int ret = OK; - if (psock == NULL || psock->s_conn == NULL) + if (psock == NULL || psock->s_type != SOCK_STREAM || + psock->s_conn == NULL) { nerr("ERROR: Invalid socket\n"); ret = -EBADF; goto errout; } - if (psock->s_type != SOCK_STREAM || !_SS_ISCONNECTED(psock->s_flags)) + if (!_SS_ISCONNECTED(psock->s_flags)) { nerr("ERROR: Not connected\n"); ret = -ENOTCONN; @@ -1100,6 +1101,17 @@ ssize_t psock_tcp_send(FAR struct socket *psock, FAR const void *buf, net_lock(); + /* Now that we have the network locked, we need to check the connection + * state again to ensure the connection is still valid. + */ + + if (!_SS_ISCONNECTED(psock->s_flags)) + { + nerr("ERROR: No longer connected\n"); + ret = -ENOTCONN; + goto errout_with_lock; + } + /* Allocate resources to receive a callback */ if (psock->s_sndcb == NULL) diff --git a/net/tcp/tcp_send_unbuffered.c b/net/tcp/tcp_send_unbuffered.c index aa3605b932b..b00d11561bc 100644 --- a/net/tcp/tcp_send_unbuffered.c +++ b/net/tcp/tcp_send_unbuffered.c @@ -463,7 +463,8 @@ ssize_t psock_tcp_send(FAR struct socket *psock, /* Verify that the sockfd corresponds to valid, allocated socket */ - if (psock == NULL || psock->s_conn == NULL) + if (psock == NULL || psock->s_type != SOCK_STREAM || + psock->s_conn == NULL) { nerr("ERROR: Invalid socket\n"); ret = -EBADF; @@ -475,7 +476,7 @@ ssize_t psock_tcp_send(FAR struct socket *psock, * guarantee the state won't change until we have the network locked. */ - if (psock->s_type != SOCK_STREAM) + if (!_SS_ISCONNECTED(psock->s_flags)) { nerr("ERROR: Not connected\n"); ret = -ENOTCONN;