Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions internal/capi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,15 @@ type listener struct {
}

type connection struct {
conn net.Conn
conn net.Conn

// localAddr and remoteAddr are recorded when the connection is
// published. A netstack TCP connection reports a nil RemoteAddr once
// its endpoint reaches CLOSED, which happens while the handle is still
// open if both directions are shut down, so Info must not ask conn.
localAddr string
remoteAddr string

readGate gate
writeGate gate
network int
Expand Down Expand Up @@ -119,6 +127,12 @@ func (c *connection) release() {
func newConnection(conn net.Conn, network int) *connection {
c := &connection{conn: conn, readGate: newGate(), writeGate: newGate(), network: network,
stopped: make(chan struct{}), pumpDone: make(chan struct{})}
if la := conn.LocalAddr(); la != nil {
c.localAddr = la.String()
}
if ra := conn.RemoteAddr(); ra != nil {
c.remoteAddr = ra.String()
}
if network == TCP {
c.reads = make(chan readResult, 1)
go c.receive()
Expand Down Expand Up @@ -486,7 +500,7 @@ func Info(id, token Handle) (out string, err error) {
case *listener:
info = map[string]any{"local_address": v.ln.Addr().String(), "network": v.network}
case *connection:
info = map[string]any{"local_address": v.conn.LocalAddr().String(), "remote_address": v.conn.RemoteAddr().String(), "network": v.network}
info = map[string]any{"local_address": v.localAddr, "remote_address": v.remoteAddr, "network": v.network}
default:
return ErrType
}
Expand Down
54 changes: 54 additions & 0 deletions internal/capi/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package capi

import (
"context"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -156,6 +157,59 @@ func TestE2ETCPRoundTripAndHalfClose(t *testing.T) {
}
}

// TestE2EInfoAfterHalfClose verifies that Info still reports a TCP
// connection's addresses after both directions have closed, at which point
// the netstack conn of the side that closed second returns a nil RemoteAddr.
func TestE2EInfoAfterHalfClose(t *testing.T) {
x := newE2E(t)
_, dialed, accepted := x.connect(TCP)
want := map[Handle]map[string]any{dialed: x.info(dialed), accepted: x.info(accepted)}

// The dialing side half-closes first. The accepting side sees EOF and
// half-closes second, which moves its endpoint through LAST_ACK to CLOSED
// while both handles stay registered.
if err := CloseWrite(dialed, x.token()); err != nil {
t.Fatal(err)
}
if _, err := Read(accepted, x.token(), make([]byte, 8)); !errors.Is(err, io.EOF) {
t.Fatalf("read after peer half close = %v; want io.EOF", err)
}
if err := CloseWrite(accepted, x.token()); err != nil {
t.Fatal(err)
}

// The endpoint state change is asynchronous. Wait until the underlying
// conn has dropped its remote address so Info is checked in the state
// that used to panic.
deadline := time.Now().Add(10 * time.Second)
for {
var gone bool
err := use(accepted, NoCancel, func(_ context.Context, _ *entry, c *connection) error {
gone = c.conn.RemoteAddr() == nil
return nil
})
if err != nil {
t.Fatal(err)
}
if gone {
break
}
if time.Now().After(deadline) {
t.Fatal("accepted conn never dropped its remote address")
}
time.Sleep(time.Millisecond)
}

for _, h := range []Handle{dialed, accepted} {
got := x.info(h)
for _, k := range []string{"local_address", "remote_address"} {
if got[k] != want[h][k] {
t.Errorf("handle %d: %s after half close = %v; want %v", h, k, got[k], want[h][k])
}
}
}
}

func TestE2EUDPRoundTripAndDeadline(t *testing.T) {
x := newE2E(t)
_, dialed, accepted := x.connect(UDP)
Expand Down
Loading