Make connect on socket returned by accept correctly error out with EISCONN

PiperOrigin-RevId: 279814493
This commit is contained in:
gVisor bot
2019-11-11 14:15:06 -08:00
parent 833dbba70d
commit 7730716800
3 changed files with 18 additions and 0 deletions
+2
View File
@@ -300,6 +300,7 @@ func (l *listenContext) createEndpointAndPerformHandshake(s *segment, opts *head
ep.mu.Lock()
ep.stack.Stats().TCP.CurrentEstablished.Increment()
ep.state = StateEstablished
ep.isConnectNotified = true
ep.mu.Unlock()
// Update the receive window scaling. We can't do it before the
@@ -539,6 +540,7 @@ func (e *endpoint) handleListenSegment(ctx *listenContext, s *segment) {
// Switch state to connected.
n.stack.Stats().TCP.CurrentEstablished.Increment()
n.state = StateEstablished
n.isConnectNotified = true
// Do the delivery in a separate goroutine so
// that we don't block the listen loop in case
+3
View File
@@ -4599,6 +4599,9 @@ func TestEndpointBindListenAcceptState(t *testing.T) {
if got, want := tcp.EndpointState(aep.State()), tcp.StateEstablished; got != want {
t.Errorf("Unexpected endpoint state: want %v, got %v", want, got)
}
if err := aep.Connect(tcpip.FullAddress{Addr: context.TestAddr, Port: context.TestPort}); err != tcpip.ErrAlreadyConnected {
t.Errorf("Unexpected error attempting to call connect on an established endpoint, got: %v, want: %v", err, tcpip.ErrAlreadyConnected)
}
// Listening endpoint remains in listen state.
if got, want := tcp.EndpointState(ep.State()), tcp.StateListen; got != want {
t.Errorf("Unexpected endpoint state: want %v, got %v", want, got)
+13
View File
@@ -130,6 +130,19 @@ void TcpSocketTest::TearDown() {
}
}
TEST_P(TcpSocketTest, ConnectOnEstablishedConnection) {
sockaddr_storage addr =
ASSERT_NO_ERRNO_AND_VALUE(InetLoopbackAddr(GetParam()));
socklen_t addrlen = sizeof(addr);
ASSERT_THAT(
connect(s_, reinterpret_cast<const struct sockaddr*>(&addr), addrlen),
SyscallFailsWithErrno(EISCONN));
ASSERT_THAT(
connect(t_, reinterpret_cast<const struct sockaddr*>(&addr), addrlen),
SyscallFailsWithErrno(EISCONN));
}
TEST_P(TcpSocketTest, DataCoalesced) {
char buf[10];