diff --git a/pkg/agent/util/syscall/syscall_windows.go b/pkg/agent/util/syscall/syscall_windows.go index c0b5d29e9ca..6af9bddde96 100644 --- a/pkg/agent/util/syscall/syscall_windows.go +++ b/pkg/agent/util/syscall/syscall_windows.go @@ -133,6 +133,12 @@ func (a *RawSockAddrInet) String() string { return a.IP().String() } +func (a *RawSockAddrInet) deepCopy() RawSockAddrInet { + sockData := [26]byte{} + copy(a.data[:], sockData[:]) + return RawSockAddrInet{Family: a.Family, data: sockData} +} + func NewRawSockAddrInetFromIP(ip net.IP) *RawSockAddrInet { sockAddrInet := new(RawSockAddrInet) if ip.To4() != nil { @@ -202,6 +208,10 @@ func (p *AddressPrefix) String() string { return p.IPNet().String() } +func (p *AddressPrefix) deepCopy() AddressPrefix { + return AddressPrefix{Prefix: p.Prefix, prefixLength: p.prefixLength} +} + func NewAddressPrefixFromIPNet(ipnet *net.IPNet) *AddressPrefix { if ipnet == nil { return nil @@ -359,6 +369,27 @@ func (n *netIO) getIPForwardTable(family uint16, ipForwardTable **MibIPForwardTa return } +func (r *MibIPForwardRow) deepCopy() MibIPForwardRow { + return MibIPForwardRow{ + Luid: r.Luid, + Index: r.Index, + SitePrefixLength: r.SitePrefixLength, + ValidLifetime: r.ValidLifetime, + PreferredLifetime: r.PreferredLifetime, + Metric: r.Metric, + Protocol: r.Protocol, + Loopback: r.Loopback, + AutoconfigureAddress: r.AutoconfigureAddress, + Publish: r.Publish, + Immortal: r.Immortal, + Age: r.Age, + Origin: r.Origin, + + DestinationPrefix: r.DestinationPrefix.deepCopy(), + NextHop: r.NextHop.deepCopy(), + } +} + func (n *netIO) ListIPForwardRows(family uint16) ([]MibIPForwardRow, error) { var table *MibIPForwardTable err := n.getIPForwardTable(family, &table) @@ -368,7 +399,16 @@ func (n *netIO) ListIPForwardRows(family uint16) ([]MibIPForwardRow, error) { if err != nil { return nil, os.NewSyscallError("iphlpapi.GetIpForwardTable", err) } - return unsafe.Slice(&table.Table[0], table.NumEntries), nil + rows := make([]MibIPForwardRow, table.NumEntries, table.NumEntries) + + pFirstRow := uintptr(unsafe.Pointer(&table.Table[0])) + rowSize := unsafe.Sizeof(table.Table[0]) + + for i := uint32(0); i < table.NumEntries; i++ { + row := *(*MibIPForwardRow)(unsafe.Pointer(pFirstRow + rowSize*uintptr(i))) + rows[i] = row.deepCopy() + } + return rows, nil } func NewIPForwardRow() *MibIPForwardRow {