mirror of
https://github.com/golang/go
synced 2024-11-13 20:10:32 -07:00
e05b48e22c
The current implementation including Go 1.5 through 1.5.2 misuses Windows API and mishandles the returned values from GetAdapterAddresses on Windows. This change fixes various issues related to network facility information by readjusting interface and interface address parsers. Updates #5395. Updates #10530. Updates #12301. Updates #12551. Updates #13542. Fixes #12691. Fixes #12811. Fixes #13476. Fixes #13544. Also fixes fragile screen scraping test cases in net_windows_test.go. Additional information for reviewers: It seems like almost all the issues above have the same root cause and it is misunderstanding of Windows API. If my interpretation of the information on MSDN is correctly, current implementation contains the following bugs: - SIO_GET_INTERFACE_LIST should not be used for IPv6. The behavior of SIO_GET_INTERFACE_LIST is different on kernels and probably it doesn't work correctly for IPv6 on old kernels such as Windows XP w/ SP2. Unfortunately MSDN doesn't describe the detail of SIO_GET_INTERFACE_LIST, but information on the net suggests so. - Fetching IP_ADAPTER_ADDRESSES structures with fixed size area may not work when using IPv6. IPv6 generates ton of interface addresses for various addressing scopes. We need to adjust the area appropriately. - PhysicalAddress field of IP_ADAPTER_ADDRESSES structure may have extra space. We cannot ignore PhysicalAddressLength field of IP_ADAPTER_ADDRESS structure. - Flags field of IP_ADAPTER_ADDRESSES structure doesn't represent any of administratively and operatinal statuses. It just represents settings for windows network adapter. - MTU field of IP_ADAPTER_ADDRESSES structure may have a uint32(-1) on 64-bit platform. We need to convert the value to interger appropriately. - IfType field of IP_ADAPTER_ADDRESSES structure is not a bit field. Bitwire operation for the field is completely wrong. - OperStatus field of IP_ADAPTER_ADDRESSES structure is not a bit field. Bitwire operation for the field is completely wrong. - IPv6IfIndex field of IP_ADAPTER_ADDRESSES structure is just a substitute for IfIndex field. We cannot prefer IPv6IfIndex to IfIndex. - Windows XP, 2003 server and below don't set OnLinkPrefixLength field of IP_ADAPTER_UNICAST_ADDRESS structure. We cannot rely on the field on old kernels. We can use FirstPrefix field of IP_ADAPTER_ADDRESSES structure and IP_ADAPTER_PREFIX structure instead. - Length field of IP_ADAPTER_{UNICAST,ANYCAST,MULTICAST}_ADDRESS sturecures doesn't represent an address prefix length. It just represents a socket address length. Change-Id: Icabdaf7bd1d41360a981d2dad0b830b02b584528 Reviewed-on: https://go-review.googlesource.com/17412 Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
133 lines
3.6 KiB
Go
133 lines
3.6 KiB
Go
// Copyright 2015 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package net
|
|
|
|
import (
|
|
"bytes"
|
|
"internal/syscall/windows"
|
|
"sort"
|
|
"testing"
|
|
)
|
|
|
|
func TestWindowsInterfaces(t *testing.T) {
|
|
aas, err := adapterAddresses()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ift, err := Interfaces()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for i, ifi := range ift {
|
|
aa := aas[i]
|
|
if len(ifi.HardwareAddr) != int(aa.PhysicalAddressLength) {
|
|
t.Errorf("got %d; want %d", len(ifi.HardwareAddr), aa.PhysicalAddressLength)
|
|
}
|
|
if ifi.MTU > 0x7fffffff {
|
|
t.Errorf("%s: got %d; want less than or equal to 1<<31 - 1", ifi.Name, ifi.MTU)
|
|
}
|
|
if ifi.Flags&FlagUp != 0 && aa.OperStatus != windows.IfOperStatusUp {
|
|
t.Errorf("%s: got %v; should not include FlagUp", ifi.Name, ifi.Flags)
|
|
}
|
|
if ifi.Flags&FlagLoopback != 0 && aa.IfType != windows.IF_TYPE_SOFTWARE_LOOPBACK {
|
|
t.Errorf("%s: got %v; should not include FlagLoopback", ifi.Name, ifi.Flags)
|
|
}
|
|
if _, _, err := addrPrefixTable(aa); err != nil {
|
|
t.Errorf("%s: %v", ifi.Name, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
type byAddrLen []IPNet
|
|
|
|
func (ps byAddrLen) Len() int { return len(ps) }
|
|
|
|
func (ps byAddrLen) Less(i, j int) bool {
|
|
if n := bytes.Compare(ps[i].IP, ps[j].IP); n != 0 {
|
|
return n < 0
|
|
}
|
|
if n := bytes.Compare(ps[i].Mask, ps[j].Mask); n != 0 {
|
|
return n < 0
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (ps byAddrLen) Swap(i, j int) { ps[i], ps[j] = ps[j], ps[i] }
|
|
|
|
var windowsAddrPrefixLenTests = []struct {
|
|
pfxs []IPNet
|
|
ip IP
|
|
out int
|
|
}{
|
|
{
|
|
[]IPNet{
|
|
{IP: IPv4(172, 16, 0, 0), Mask: IPv4Mask(255, 255, 0, 0)},
|
|
{IP: IPv4(192, 168, 0, 0), Mask: IPv4Mask(255, 255, 255, 0)},
|
|
{IP: IPv4(192, 168, 0, 0), Mask: IPv4Mask(255, 255, 255, 128)},
|
|
{IP: IPv4(192, 168, 0, 0), Mask: IPv4Mask(255, 255, 255, 192)},
|
|
},
|
|
IPv4(192, 168, 0, 1),
|
|
26,
|
|
},
|
|
{
|
|
[]IPNet{
|
|
{IP: ParseIP("2001:db8::"), Mask: IPMask(ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:fff0"))},
|
|
{IP: ParseIP("2001:db8::"), Mask: IPMask(ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:fff8"))},
|
|
{IP: ParseIP("2001:db8::"), Mask: IPMask(ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffc"))},
|
|
},
|
|
ParseIP("2001:db8::1"),
|
|
126,
|
|
},
|
|
|
|
// Fallback cases. It may happen on Windows XP or 2003 server.
|
|
{
|
|
[]IPNet{
|
|
{IP: IPv4(127, 0, 0, 0).To4(), Mask: IPv4Mask(255, 0, 0, 0)},
|
|
{IP: IPv4(10, 0, 0, 0).To4(), Mask: IPv4Mask(255, 0, 0, 0)},
|
|
{IP: IPv4(172, 16, 0, 0).To4(), Mask: IPv4Mask(255, 255, 0, 0)},
|
|
{IP: IPv4(192, 168, 255, 0), Mask: IPv4Mask(255, 255, 255, 0)},
|
|
{IP: IPv4zero, Mask: IPv4Mask(0, 0, 0, 0)},
|
|
},
|
|
IPv4(192, 168, 0, 1),
|
|
8 * IPv4len,
|
|
},
|
|
{
|
|
nil,
|
|
IPv4(192, 168, 0, 1),
|
|
8 * IPv4len,
|
|
},
|
|
{
|
|
[]IPNet{
|
|
{IP: IPv6loopback, Mask: IPMask(ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"))},
|
|
{IP: ParseIP("2001:db8:1::"), Mask: IPMask(ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:fff0"))},
|
|
{IP: ParseIP("2001:db8:2::"), Mask: IPMask(ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:fff8"))},
|
|
{IP: ParseIP("2001:db8:3::"), Mask: IPMask(ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffc"))},
|
|
{IP: IPv6unspecified, Mask: IPMask(ParseIP("::"))},
|
|
},
|
|
ParseIP("2001:db8::1"),
|
|
8 * IPv6len,
|
|
},
|
|
{
|
|
nil,
|
|
ParseIP("2001:db8::1"),
|
|
8 * IPv6len,
|
|
},
|
|
}
|
|
|
|
func TestWindowsAddrPrefixLen(t *testing.T) {
|
|
for i, tt := range windowsAddrPrefixLenTests {
|
|
sort.Sort(byAddrLen(tt.pfxs))
|
|
l := addrPrefixLen(tt.pfxs, tt.ip)
|
|
if l != tt.out {
|
|
t.Errorf("#%d: got %d; want %d", i, l, tt.out)
|
|
}
|
|
sort.Sort(sort.Reverse(byAddrLen(tt.pfxs)))
|
|
l = addrPrefixLen(tt.pfxs, tt.ip)
|
|
if l != tt.out {
|
|
t.Errorf("#%d: got %d; want %d", i, l, tt.out)
|
|
}
|
|
}
|
|
}
|