Skip to content

Commit

Permalink
net, internal/syscall/windows: fix interface and address identificati…
Browse files Browse the repository at this point in the history
…on on windows

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>
  • Loading branch information
cixtor committed Dec 10, 2015
1 parent c2ef005 commit e05b48e
Show file tree
Hide file tree
Showing 6 changed files with 366 additions and 160 deletions.
14 changes: 12 additions & 2 deletions src/internal/syscall/windows/syscall_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,17 @@ import "syscall"

const GAA_FLAG_INCLUDE_PREFIX = 0x00000010

const IF_TYPE_SOFTWARE_LOOPBACK = 24
const (
IF_TYPE_OTHER = 1
IF_TYPE_ETHERNET_CSMACD = 6
IF_TYPE_ISO88025_TOKENRING = 9
IF_TYPE_PPP = 23
IF_TYPE_SOFTWARE_LOOPBACK = 24
IF_TYPE_ATM = 37
IF_TYPE_IEEE80211 = 71
IF_TYPE_TUNNEL = 131
IF_TYPE_IEEE1394 = 144
)

type SocketAddress struct {
Sockaddr *syscall.RawSockaddrAny
Expand Down Expand Up @@ -94,7 +104,7 @@ const (
IfOperStatusLowerLayerDown = 7
)

//sys GetAdaptersAddresses(family uint32, flags uint32, reserved uintptr, adapterAddresses *IpAdapterAddresses, sizeOfPointer *uint32) (errcode error) = iphlpapi.GetAdaptersAddresses
//sys GetAdaptersAddresses(family uint32, flags uint32, reserved uintptr, adapterAddresses *IpAdapterAddresses, sizePointer *uint32) (errcode error) = iphlpapi.GetAdaptersAddresses
//sys GetComputerNameEx(nameformat uint32, buf *uint16, n *uint32) (err error) = GetComputerNameExW
//sys MoveFileEx(from *uint16, to *uint16, flags uint32) (err error) = MoveFileExW

Expand Down
4 changes: 2 additions & 2 deletions src/internal/syscall/windows/zsyscall_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ var (
procMoveFileExW = modkernel32.NewProc("MoveFileExW")
)

func GetAdaptersAddresses(family uint32, flags uint32, reserved uintptr, adapterAddresses *IpAdapterAddresses, sizeOfPointer *uint32) (errcode error) {
r0, _, _ := syscall.Syscall6(procGetAdaptersAddresses.Addr(), 5, uintptr(family), uintptr(flags), uintptr(reserved), uintptr(unsafe.Pointer(adapterAddresses)), uintptr(unsafe.Pointer(sizeOfPointer)), 0)
func GetAdaptersAddresses(family uint32, flags uint32, reserved uintptr, adapterAddresses *IpAdapterAddresses, sizePointer *uint32) (errcode error) {
r0, _, _ := syscall.Syscall6(procGetAdaptersAddresses.Addr(), 5, uintptr(family), uintptr(flags), uintptr(reserved), uintptr(unsafe.Pointer(adapterAddresses)), uintptr(unsafe.Pointer(sizePointer)), 0)
if r0 != 0 {
errcode = syscall.Errno(r0)
}
Expand Down
30 changes: 26 additions & 4 deletions src/net/interface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,18 +188,39 @@ func testAddrs(t *testing.T, ifat []Addr) (naf4, naf6 int) {
prefixLen, maxPrefixLen := ifa.Mask.Size()
if ifa.IP.To4() != nil {
if 0 >= prefixLen || prefixLen > 8*IPv4len || maxPrefixLen != 8*IPv4len {
t.Errorf("unexpected prefix length: %v/%v", prefixLen, maxPrefixLen)
t.Errorf("unexpected prefix length: %d/%d", prefixLen, maxPrefixLen)
continue
}
if ifa.IP.IsLoopback() && (prefixLen != 8 && prefixLen != 8*IPv4len) { // see RFC 1122
t.Errorf("unexpected prefix length for IPv4 loopback: %d/%d", prefixLen, maxPrefixLen)
continue
}
naf4++
} else if ifa.IP.To16() != nil {
}
if ifa.IP.To16() != nil && ifa.IP.To4() == nil {
if 0 >= prefixLen || prefixLen > 8*IPv6len || maxPrefixLen != 8*IPv6len {
t.Errorf("unexpected prefix length: %v/%v", prefixLen, maxPrefixLen)
t.Errorf("unexpected prefix length: %d/%d", prefixLen, maxPrefixLen)
continue
}
if ifa.IP.IsLoopback() && prefixLen != 8*IPv6len { // see RFC 4291
t.Errorf("unexpected prefix length for IPv6 loopback: %d/%d", prefixLen, maxPrefixLen)
continue
}
naf6++
}
t.Logf("interface address %q", ifa.String())
case *IPAddr:
if ifa == nil || ifa.IP == nil || ifa.IP.IsUnspecified() || ifa.IP.IsMulticast() {
t.Errorf("unexpected value: %+v", ifa)
continue
}
if ifa.IP.To4() != nil {
naf4++
}
if ifa.IP.To16() != nil && ifa.IP.To4() == nil {
naf6++
}
t.Logf("interface address %s", ifa.String())
default:
t.Errorf("unexpected type: %T", ifa)
}
Expand All @@ -217,7 +238,8 @@ func testMulticastAddrs(t *testing.T, ifmat []Addr) (nmaf4, nmaf6 int) {
}
if ifma.IP.To4() != nil {
nmaf4++
} else if ifma.IP.To16() != nil {
}
if ifma.IP.To16() != nil && ifma.IP.To4() == nil {
nmaf6++
}
t.Logf("joined group address %q", ifma.String())
Expand Down
Loading

0 comments on commit e05b48e

Please sign in to comment.