Skip to content

Commit

Permalink
Add an integration test for istio like redirect.
Browse files Browse the repository at this point in the history
Updates #6441,#6317

PiperOrigin-RevId: 404872327
  • Loading branch information
hbhasker authored and gvisor-bot committed Oct 21, 2021
1 parent cfcd3eb commit 207221f
Show file tree
Hide file tree
Showing 4 changed files with 415 additions and 5 deletions.
24 changes: 19 additions & 5 deletions pkg/tcpip/adapters/gonet/gonet.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"bytes"
"context"
"errors"
"fmt"
"io"
"net"
"time"
Expand Down Expand Up @@ -471,9 +472,9 @@ func DialTCP(s *stack.Stack, addr tcpip.FullAddress, network tcpip.NetworkProtoc
return DialContextTCP(context.Background(), s, addr, network)
}

// DialContextTCP creates a new TCPConn connected to the specified address
// with the option of adding cancellation and timeouts.
func DialContextTCP(ctx context.Context, s *stack.Stack, addr tcpip.FullAddress, network tcpip.NetworkProtocolNumber) (*TCPConn, error) {
// DialTCPWithBind creates a new TCPConn connected to the specified
// remoteAddress with its local address bound to localAddr.
func DialTCPWithBind(ctx context.Context, s *stack.Stack, localAddr, remoteAddr tcpip.FullAddress, network tcpip.NetworkProtocolNumber) (*TCPConn, error) {
// Create TCP endpoint, then connect.
var wq waiter.Queue
ep, err := s.NewEndpoint(tcp.ProtocolNumber, network, &wq)
Expand All @@ -494,7 +495,14 @@ func DialContextTCP(ctx context.Context, s *stack.Stack, addr tcpip.FullAddress,
default:
}

err = ep.Connect(addr)
// Bind before connect if requested.
if localAddr != (tcpip.FullAddress{}) {
if err = ep.Bind(localAddr); err != nil {
return nil, fmt.Errorf("ep.Bind(%+v) = %s", localAddr, err)
}
}

err = ep.Connect(remoteAddr)
if _, ok := err.(*tcpip.ErrConnectStarted); ok {
select {
case <-ctx.Done():
Expand All @@ -510,14 +518,20 @@ func DialContextTCP(ctx context.Context, s *stack.Stack, addr tcpip.FullAddress,
return nil, &net.OpError{
Op: "connect",
Net: "tcp",
Addr: fullToTCPAddr(addr),
Addr: fullToTCPAddr(remoteAddr),
Err: errors.New(err.String()),
}
}

return NewTCPConn(&wq, ep), nil
}

// DialContextTCP creates a new TCPConn connected to the specified address
// with the option of adding cancellation and timeouts.
func DialContextTCP(ctx context.Context, s *stack.Stack, addr tcpip.FullAddress, network tcpip.NetworkProtocolNumber) (*TCPConn, error) {
return DialTCPWithBind(ctx, s, tcpip.FullAddress{} /* localAddr */, addr /* remoteAddr */, network)
}

// A UDPConn is a wrapper around a UDP tcpip.Endpoint that implements
// net.Conn and net.PacketConn.
type UDPConn struct {
Expand Down
9 changes: 9 additions & 0 deletions pkg/tcpip/header/ipv4.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,15 @@ var IPv4EmptySubnet = func() tcpip.Subnet {
return subnet
}()

// IPv4LoopbackSubnet is the loopback subnet for IPv4.
var IPv4LoopbackSubnet = func() tcpip.Subnet {
subnet, err := tcpip.NewSubnet(tcpip.Address("\x7f\x00\x00\x00"), tcpip.AddressMask("\xff\x00\x00\x00"))
if err != nil {
panic(err)
}
return subnet
}()

// IPVersion returns the version of IP used in the given packet. It returns -1
// if the packet is not large enough to contain the version field.
func IPVersion(b []byte) int {
Expand Down
22 changes: 22 additions & 0 deletions pkg/tcpip/tests/integration/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,25 @@ go_test(
"@com_github_google_go_cmp//cmp:go_default_library",
],
)

go_test(
name = "istio_test",
size = "small",
srcs = ["istio_test.go"],
deps = [
"//pkg/context",
"//pkg/rand",
"//pkg/sync",
"//pkg/tcpip",
"//pkg/tcpip/adapters/gonet",
"//pkg/tcpip/header",
"//pkg/tcpip/link/loopback",
"//pkg/tcpip/link/pipe",
"//pkg/tcpip/link/sniffer",
"//pkg/tcpip/network/ipv4",
"//pkg/tcpip/stack",
"//pkg/tcpip/testutil",
"//pkg/tcpip/transport/tcp",
"@com_github_google_go_cmp//cmp:go_default_library",
],
)
Loading

0 comments on commit 207221f

Please sign in to comment.