Skip to content

Commit 6c5ce5c

Browse files
committed
pkg/hostagent: Aware forwarding guest address is IPv4 or IPv6
Signed-off-by: Norio Nomura <norio.nomura@gmail.com>
1 parent b8d4d79 commit 6c5ce5c

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

pkg/hostagent/hostagent.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -922,10 +922,28 @@ func (a *HostAgent) processGuestAgentEvents(ctx context.Context, client *guestag
922922
if guestIP == nil {
923923
return portfwd.DialContextToGRPCTunnel(client)(ctx, network, guestAddress)
924924
}
925+
guestIPv4 := a.GuestIPv4()
926+
guestIPv6 := a.GuestIPv6()
927+
// Check if the host part of guestAddress is either unspecified address or matches the known guest IP.
928+
// If so, replace it with the known guest IP to avoid issues with dual-stack setups and DNS resolution.
929+
// Otherwise, fall back to the gRPC tunnel.
925930
if host, _, err := net.SplitHostPort(guestAddress); err != nil {
926931
return nil, err
927-
} else if ip := net.ParseIP(host); ip.IsUnspecified() || ip.Equal(guestIP) {
928-
return DialContextToGuestIP(guestIP)(ctx, network, guestAddress)
932+
} else if ip := net.ParseIP(host); ip.IsUnspecified() || ip.Equal(guestIPv4) || ip.Equal(guestIPv6) {
933+
if ip.To4() != nil {
934+
if guestIPv4 != nil {
935+
return DialContextToGuestIP(guestIPv4)(ctx, network, guestAddress)
936+
} else if guestIPv4 = a.GuestIPv6().To4(); guestIPv4 != nil {
937+
return DialContextToGuestIP(guestIPv4)(ctx, network, guestAddress)
938+
}
939+
} else if ip.To16() != nil {
940+
if guestIPv6 != nil {
941+
return DialContextToGuestIP(guestIPv6)(ctx, network, guestAddress)
942+
} else if guestIPv6 = a.GuestIPv4().To16(); guestIPv6 != nil {
943+
return DialContextToGuestIP(guestIPv6)(ctx, network, guestAddress)
944+
}
945+
}
946+
// If we reach here, it means we couldn't find a suitable guest IP
929947
}
930948
return portfwd.DialContextToGRPCTunnel(client)(ctx, network, guestAddress)
931949
}

0 commit comments

Comments
 (0)