2016-03-01 15:57:46 -07:00
|
|
|
// Copyright 2011 The Go Authors. All rights reserved.
|
2011-06-22 17:54:57 -06:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
2015-01-19 00:56:04 -07:00
|
|
|
"internal/syscall/windows"
|
net: fix inconsistent errors
These a series of changes fix inconsistent errors on the package net
APIs. Now almost all the APIs return OpError as a common error type
except Lookup, Resolve and Parse APIs. The Lookup, Resolve and Parse
APIs return more specific errors such as DNSError, AddrError or
ParseError.
An OpError may contain nested error information. For example, Dial may
return an OpError containing a DNSError, AddrError, unexposed type/value
or other package's type/value like the following:
OpError{/* dial info */, Err: &DNSError{}}
OpError{/* dial info */, Err: &AddrError{}}
OpError{/* dial info */, Err: <unexposed type or value>}
OpError{/* dial info */, Err: <other package's type or value>}
and Read and Write may return an OpError containing other OpError when
an application uses io.Copy or similar:
OpError{/* for io.Reader */, Err: &OpError{/* for io.Writer */}}
When an endpoint is created for connection-oriented byte-stream
protocols, Read may return an io.EOF when the connection is closed by
remote endpoint.
Fixes #4856.
A series of changes:
- net: fix inconsistent error values on Dial, Listen partially
https://go.googlesource.com/go/+/89b7c66d0d14462fd7893be4290bdfe5f9063ae1
- net: fix inconsistent error values on Read
https://go.googlesource.com/go/+/ec1144423f45e010c72363fe59291d43214b6e31
- net: fix inconsistent error values on Write
https://go.googlesource.com/go/+/11b5f98bf0d5eb8854f735cc332c912725070214
- net: fix inconsistent error values on Close
https://go.googlesource.com/go/+/310db63c5bc121e7bfccb494c01a6b91a257e7fc
- net: fix inconsistent error values on Accept
https://go.googlesource.com/go/+/4540e162b1aefda8157372764ad3d290a414ef1d
- net: fix inconsistent error values on File
https://go.googlesource.com/go/+/885111365ba0a74421059bfbd18f4c57c1e70332
- net: fix inconsistent error values on setters
https://go.googlesource.com/go/+/2173a27903897c481b0a0daf3ca3e0a0685701db
- net: fix inconsistent error values on Interface
https://go.googlesource.com/go/+/456cf0f22c93e1a6654980f4a48a564555f6c8a2
- net: fix inconsistent error values on Lookup
https://go.googlesource.com/go/+/0fc582e87942b2e52bed751b6c56660ba99e9a7d
- net: add Source field to OpError
https://go.googlesource.com/go/+/afd2d2b6df3ebfe99faf347030f15adfdf422fa0
Change-Id: Id678e369088dc9fbe9073cfe7ff8a8754a57d61f
Reviewed-on: https://go-review.googlesource.com/9236
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2015-04-21 06:20:15 -06:00
|
|
|
"os"
|
2011-06-22 17:54:57 -06:00
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2015-12-04 03:06:01 -07:00
|
|
|
// supportsVistaIP reports whether the platform implements new IP
|
|
|
|
// stack and ABIs supported on Windows Vista and above.
|
|
|
|
var supportsVistaIP bool
|
2011-06-22 17:54:57 -06:00
|
|
|
|
2015-12-04 03:06:01 -07:00
|
|
|
func init() {
|
|
|
|
supportsVistaIP = probeWindowsIPStack()
|
2011-06-22 17:54:57 -06:00
|
|
|
}
|
|
|
|
|
2015-12-04 03:06:01 -07:00
|
|
|
func probeWindowsIPStack() (supportsVistaIP bool) {
|
|
|
|
v, err := syscall.GetVersion()
|
2012-01-31 08:36:45 -07:00
|
|
|
if err != nil {
|
2015-12-04 03:06:01 -07:00
|
|
|
return true // Windows 10 and above will deprecate this API
|
2011-06-22 17:54:57 -06:00
|
|
|
}
|
2017-04-27 03:58:37 -06:00
|
|
|
return byte(v) >= 6 // major version of Windows Vista is 6
|
2015-01-19 00:56:04 -07:00
|
|
|
}
|
|
|
|
|
2015-12-04 03:06:01 -07:00
|
|
|
// adapterAddresses returns a list of IP adapter and address
|
|
|
|
// structures. The structure contains an IP adapter and flattened
|
|
|
|
// multiple IP addresses including unicast, anycast and multicast
|
|
|
|
// addresses.
|
|
|
|
func adapterAddresses() ([]*windows.IpAdapterAddresses, error) {
|
|
|
|
var b []byte
|
|
|
|
l := uint32(15000) // recommended initial size
|
|
|
|
for {
|
|
|
|
b = make([]byte, l)
|
|
|
|
err := windows.GetAdaptersAddresses(syscall.AF_UNSPEC, windows.GAA_FLAG_INCLUDE_PREFIX, 0, (*windows.IpAdapterAddresses)(unsafe.Pointer(&b[0])), &l)
|
|
|
|
if err == nil {
|
|
|
|
if l == 0 {
|
|
|
|
return nil, nil
|
2015-01-19 00:56:04 -07:00
|
|
|
}
|
2015-12-04 03:06:01 -07:00
|
|
|
break
|
2015-01-19 00:56:04 -07:00
|
|
|
}
|
2015-12-04 03:06:01 -07:00
|
|
|
if err.(syscall.Errno) != syscall.ERROR_BUFFER_OVERFLOW {
|
|
|
|
return nil, os.NewSyscallError("getadaptersaddresses", err)
|
|
|
|
}
|
|
|
|
if l <= uint32(len(b)) {
|
|
|
|
return nil, os.NewSyscallError("getadaptersaddresses", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var aas []*windows.IpAdapterAddresses
|
|
|
|
for aa := (*windows.IpAdapterAddresses)(unsafe.Pointer(&b[0])); aa != nil; aa = aa.Next {
|
|
|
|
aas = append(aas, aa)
|
2015-01-19 00:56:04 -07:00
|
|
|
}
|
2015-12-04 03:06:01 -07:00
|
|
|
return aas, nil
|
2011-06-22 17:54:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the ifindex is zero, interfaceTable returns mappings of all
|
2016-03-01 16:21:55 -07:00
|
|
|
// network interfaces. Otherwise it returns a mapping of a specific
|
2011-06-22 17:54:57 -06:00
|
|
|
// interface.
|
2011-11-01 20:05:34 -06:00
|
|
|
func interfaceTable(ifindex int) ([]Interface, error) {
|
2015-12-04 03:06:01 -07:00
|
|
|
aas, err := adapterAddresses()
|
2012-01-31 08:36:45 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2011-06-22 17:54:57 -06:00
|
|
|
}
|
|
|
|
var ift []Interface
|
2015-12-04 03:06:01 -07:00
|
|
|
for _, aa := range aas {
|
|
|
|
index := aa.IfIndex
|
2015-12-13 20:16:13 -07:00
|
|
|
if index == 0 { // ipv6IfIndex is a substitute for ifIndex
|
2015-12-04 03:06:01 -07:00
|
|
|
index = aa.Ipv6IfIndex
|
2015-01-19 00:56:04 -07:00
|
|
|
}
|
2011-06-22 17:54:57 -06:00
|
|
|
if ifindex == 0 || ifindex == int(index) {
|
2015-12-04 03:06:01 -07:00
|
|
|
ifi := Interface{
|
|
|
|
Index: int(index),
|
|
|
|
Name: syscall.UTF16ToString((*(*[10000]uint16)(unsafe.Pointer(aa.FriendlyName)))[:]),
|
2015-01-19 00:56:04 -07:00
|
|
|
}
|
2015-12-04 03:06:01 -07:00
|
|
|
if aa.OperStatus == windows.IfOperStatusUp {
|
|
|
|
ifi.Flags |= FlagUp
|
2015-01-19 00:56:04 -07:00
|
|
|
}
|
2015-12-04 03:06:01 -07:00
|
|
|
// For now we need to infer link-layer service
|
|
|
|
// capabilities from media types.
|
|
|
|
// We will be able to use
|
|
|
|
// MIB_IF_ROW2.AccessType once we drop support
|
|
|
|
// for Windows XP.
|
|
|
|
switch aa.IfType {
|
|
|
|
case windows.IF_TYPE_ETHERNET_CSMACD, windows.IF_TYPE_ISO88025_TOKENRING, windows.IF_TYPE_IEEE80211, windows.IF_TYPE_IEEE1394:
|
|
|
|
ifi.Flags |= FlagBroadcast | FlagMulticast
|
|
|
|
case windows.IF_TYPE_PPP, windows.IF_TYPE_TUNNEL:
|
|
|
|
ifi.Flags |= FlagPointToPoint | FlagMulticast
|
|
|
|
case windows.IF_TYPE_SOFTWARE_LOOPBACK:
|
|
|
|
ifi.Flags |= FlagLoopback | FlagMulticast
|
|
|
|
case windows.IF_TYPE_ATM:
|
2015-12-17 20:39:57 -07:00
|
|
|
ifi.Flags |= FlagBroadcast | FlagPointToPoint | FlagMulticast // assume all services available; LANE, point-to-point and point-to-multipoint
|
2015-01-19 00:56:04 -07:00
|
|
|
}
|
2015-12-04 03:06:01 -07:00
|
|
|
if aa.Mtu == 0xffffffff {
|
|
|
|
ifi.MTU = -1
|
|
|
|
} else {
|
|
|
|
ifi.MTU = int(aa.Mtu)
|
2011-06-22 17:54:57 -06:00
|
|
|
}
|
2015-12-04 03:06:01 -07:00
|
|
|
if aa.PhysicalAddressLength > 0 {
|
|
|
|
ifi.HardwareAddr = make(HardwareAddr, aa.PhysicalAddressLength)
|
|
|
|
copy(ifi.HardwareAddr, aa.PhysicalAddress[:])
|
2015-01-19 00:56:04 -07:00
|
|
|
}
|
2011-06-22 17:54:57 -06:00
|
|
|
ift = append(ift, ifi)
|
2015-12-04 03:06:01 -07:00
|
|
|
if ifindex == ifi.Index {
|
2015-01-19 00:56:04 -07:00
|
|
|
break
|
|
|
|
}
|
2011-06-22 17:54:57 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ift, nil
|
|
|
|
}
|
|
|
|
|
2013-02-27 22:58:41 -07:00
|
|
|
// If the ifi is nil, interfaceAddrTable returns addresses for all
|
2016-03-01 16:21:55 -07:00
|
|
|
// network interfaces. Otherwise it returns addresses for a specific
|
2013-02-27 22:58:41 -07:00
|
|
|
// interface.
|
|
|
|
func interfaceAddrTable(ifi *Interface) ([]Addr, error) {
|
2015-12-04 03:06:01 -07:00
|
|
|
aas, err := adapterAddresses()
|
2012-01-31 08:36:45 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2011-06-22 17:54:57 -06:00
|
|
|
}
|
|
|
|
var ifat []Addr
|
2015-12-04 03:06:01 -07:00
|
|
|
for _, aa := range aas {
|
|
|
|
index := aa.IfIndex
|
2015-12-13 20:16:13 -07:00
|
|
|
if index == 0 { // ipv6IfIndex is a substitute for ifIndex
|
2015-12-04 03:06:01 -07:00
|
|
|
index = aa.Ipv6IfIndex
|
|
|
|
}
|
|
|
|
var pfx4, pfx6 []IPNet
|
|
|
|
if !supportsVistaIP {
|
|
|
|
pfx4, pfx6, err = addrPrefixTable(aa)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-01-19 00:56:04 -07:00
|
|
|
}
|
2013-02-27 22:58:41 -07:00
|
|
|
if ifi == nil || ifi.Index == int(index) {
|
2015-12-04 03:06:01 -07:00
|
|
|
for puni := aa.FirstUnicastAddress; puni != nil; puni = puni.Next {
|
|
|
|
sa, err := puni.Address.Sockaddr.Sockaddr()
|
|
|
|
if err != nil {
|
|
|
|
return nil, os.NewSyscallError("sockaddr", err)
|
|
|
|
}
|
|
|
|
var l int
|
|
|
|
switch sa := sa.(type) {
|
|
|
|
case *syscall.SockaddrInet4:
|
|
|
|
if supportsVistaIP {
|
|
|
|
l = int(puni.OnLinkPrefixLength)
|
|
|
|
} else {
|
|
|
|
l = addrPrefixLen(pfx4, IP(sa.Addr[:]))
|
2015-01-19 00:56:04 -07:00
|
|
|
}
|
2015-12-17 20:39:57 -07:00
|
|
|
ifat = append(ifat, &IPNet{IP: IPv4(sa.Addr[0], sa.Addr[1], sa.Addr[2], sa.Addr[3]), Mask: CIDRMask(l, 8*IPv4len)})
|
2015-12-04 03:06:01 -07:00
|
|
|
case *syscall.SockaddrInet6:
|
|
|
|
if supportsVistaIP {
|
|
|
|
l = int(puni.OnLinkPrefixLength)
|
|
|
|
} else {
|
|
|
|
l = addrPrefixLen(pfx6, IP(sa.Addr[:]))
|
|
|
|
}
|
|
|
|
ifa := &IPNet{IP: make(IP, IPv6len), Mask: CIDRMask(l, 8*IPv6len)}
|
2016-04-27 20:50:48 -06:00
|
|
|
copy(ifa.IP, sa.Addr[:])
|
2015-12-04 03:06:01 -07:00
|
|
|
ifat = append(ifat, ifa)
|
2015-01-19 00:56:04 -07:00
|
|
|
}
|
|
|
|
}
|
2015-12-04 03:06:01 -07:00
|
|
|
for pany := aa.FirstAnycastAddress; pany != nil; pany = pany.Next {
|
|
|
|
sa, err := pany.Address.Sockaddr.Sockaddr()
|
|
|
|
if err != nil {
|
|
|
|
return nil, os.NewSyscallError("sockaddr", err)
|
|
|
|
}
|
|
|
|
switch sa := sa.(type) {
|
|
|
|
case *syscall.SockaddrInet4:
|
2015-12-17 20:39:57 -07:00
|
|
|
ifat = append(ifat, &IPAddr{IP: IPv4(sa.Addr[0], sa.Addr[1], sa.Addr[2], sa.Addr[3])})
|
2015-12-04 03:06:01 -07:00
|
|
|
case *syscall.SockaddrInet6:
|
|
|
|
ifa := &IPAddr{IP: make(IP, IPv6len)}
|
|
|
|
copy(ifa.IP, sa.Addr[:])
|
|
|
|
ifat = append(ifat, ifa)
|
2015-01-19 00:56:04 -07:00
|
|
|
}
|
2011-06-22 17:54:57 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ifat, nil
|
|
|
|
}
|
2011-08-03 22:22:52 -06:00
|
|
|
|
2015-12-04 03:06:01 -07:00
|
|
|
func addrPrefixTable(aa *windows.IpAdapterAddresses) (pfx4, pfx6 []IPNet, err error) {
|
|
|
|
for p := aa.FirstPrefix; p != nil; p = p.Next {
|
|
|
|
sa, err := p.Address.Sockaddr.Sockaddr()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, os.NewSyscallError("sockaddr", err)
|
|
|
|
}
|
|
|
|
switch sa := sa.(type) {
|
|
|
|
case *syscall.SockaddrInet4:
|
|
|
|
pfx := IPNet{IP: IP(sa.Addr[:]), Mask: CIDRMask(int(p.PrefixLength), 8*IPv4len)}
|
|
|
|
pfx4 = append(pfx4, pfx)
|
|
|
|
case *syscall.SockaddrInet6:
|
|
|
|
pfx := IPNet{IP: IP(sa.Addr[:]), Mask: CIDRMask(int(p.PrefixLength), 8*IPv6len)}
|
|
|
|
pfx6 = append(pfx6, pfx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// addrPrefixLen returns an appropriate prefix length in bits for ip
|
|
|
|
// from pfxs. It returns 32 or 128 when no appropriate on-link address
|
|
|
|
// prefix found.
|
|
|
|
//
|
|
|
|
// NOTE: This is pretty naive implementation that contains many
|
|
|
|
// allocations and non-effective linear search, and should not be used
|
|
|
|
// freely.
|
|
|
|
func addrPrefixLen(pfxs []IPNet, ip IP) int {
|
|
|
|
var l int
|
|
|
|
var cand *IPNet
|
|
|
|
for i := range pfxs {
|
|
|
|
if !pfxs[i].Contains(ip) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if cand == nil {
|
|
|
|
l, _ = pfxs[i].Mask.Size()
|
|
|
|
cand = &pfxs[i]
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
m, _ := pfxs[i].Mask.Size()
|
|
|
|
if m > l {
|
|
|
|
l = m
|
|
|
|
cand = &pfxs[i]
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if l > 0 {
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
if ip.To4() != nil {
|
|
|
|
return 8 * IPv4len
|
|
|
|
}
|
|
|
|
return 8 * IPv6len
|
|
|
|
}
|
|
|
|
|
2013-02-27 22:58:41 -07:00
|
|
|
// interfaceMulticastAddrTable returns addresses for a specific
|
|
|
|
// interface.
|
|
|
|
func interfaceMulticastAddrTable(ifi *Interface) ([]Addr, error) {
|
2015-12-04 03:06:01 -07:00
|
|
|
aas, err := adapterAddresses()
|
2015-01-19 00:56:04 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var ifat []Addr
|
2015-12-04 03:06:01 -07:00
|
|
|
for _, aa := range aas {
|
|
|
|
index := aa.IfIndex
|
2015-12-13 20:16:13 -07:00
|
|
|
if index == 0 { // ipv6IfIndex is a substitute for ifIndex
|
2015-12-04 03:06:01 -07:00
|
|
|
index = aa.Ipv6IfIndex
|
2015-01-19 00:56:04 -07:00
|
|
|
}
|
|
|
|
if ifi == nil || ifi.Index == int(index) {
|
2015-12-04 03:06:01 -07:00
|
|
|
for pmul := aa.FirstMulticastAddress; pmul != nil; pmul = pmul.Next {
|
|
|
|
sa, err := pmul.Address.Sockaddr.Sockaddr()
|
|
|
|
if err != nil {
|
|
|
|
return nil, os.NewSyscallError("sockaddr", err)
|
|
|
|
}
|
|
|
|
switch sa := sa.(type) {
|
|
|
|
case *syscall.SockaddrInet4:
|
2015-12-17 20:39:57 -07:00
|
|
|
ifat = append(ifat, &IPAddr{IP: IPv4(sa.Addr[0], sa.Addr[1], sa.Addr[2], sa.Addr[3])})
|
2015-12-04 03:06:01 -07:00
|
|
|
case *syscall.SockaddrInet6:
|
|
|
|
ifa := &IPAddr{IP: make(IP, IPv6len)}
|
|
|
|
copy(ifa.IP, sa.Addr[:])
|
|
|
|
ifat = append(ifat, ifa)
|
2015-01-19 00:56:04 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ifat, nil
|
2011-08-03 22:22:52 -06:00
|
|
|
}
|