2016-03-01 15:57:46 -07:00
|
|
|
// Copyright 2011 The Go Authors. All rights reserved.
|
2011-06-03 12:35:42 -06:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2013-08-23 10:18:22 -06:00
|
|
|
// +build darwin dragonfly freebsd netbsd openbsd
|
build: add build comments to core packages
The go/build package already recognizes
system-specific file names like
mycode_darwin.go
mycode_darwin_386.go
mycode_386.s
However, it is also common to write files that
apply to multiple architectures, so a recent CL added
to go/build the ability to process comments
listing a set of conditions for building. For example:
// +build darwin freebsd openbsd/386
says that this file should be compiled only on
OS X, FreeBSD, or 32-bit x86 OpenBSD systems.
These conventions are not yet documented
(hence this long CL description).
This CL adds build comments to the multi-system
files in the core library, a step toward making it
possible to use go/build to build them.
With this change go/build can handle crypto/rand,
exec, net, path/filepath, os/user, and time.
os and syscall need additional adjustments.
R=golang-dev, r, gri, r, gustavo
CC=golang-dev
https://golang.org/cl/5011046
2011-09-15 14:48:57 -06:00
|
|
|
|
2011-06-03 12:35:42 -06:00
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
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-03 12:35:42 -06:00
|
|
|
"syscall"
|
2011-06-07 14:06:36 -06:00
|
|
|
"unsafe"
|
2011-06-03 12:35:42 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// If the ifindex is zero, interfaceTable returns mappings of all
|
2012-02-01 17:19:36 -07:00
|
|
|
// network interfaces. Otherwise it returns a mapping of a specific
|
2011-06-03 12:35:42 -06:00
|
|
|
// interface.
|
2011-11-01 20:05:34 -06:00
|
|
|
func interfaceTable(ifindex int) ([]Interface, error) {
|
2015-12-05 00:28:25 -07:00
|
|
|
tab, err := syscall.RouteRIB(syscall.NET_RT_IFLIST, ifindex)
|
2011-12-21 05:39:00 -07:00
|
|
|
if err != nil {
|
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
|
|
|
return nil, os.NewSyscallError("routerib", err)
|
2011-06-03 12:35:42 -06:00
|
|
|
}
|
2011-12-21 05:39:00 -07:00
|
|
|
msgs, err := syscall.ParseRoutingMessage(tab)
|
|
|
|
if err != nil {
|
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
|
|
|
return nil, os.NewSyscallError("parseroutingmessage", err)
|
2011-06-03 12:35:42 -06:00
|
|
|
}
|
2013-02-27 22:58:41 -07:00
|
|
|
return parseInterfaceTable(ifindex, msgs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseInterfaceTable(ifindex int, msgs []syscall.RoutingMessage) ([]Interface, error) {
|
2012-02-02 15:40:03 -07:00
|
|
|
var ift []Interface
|
2013-02-27 22:58:41 -07:00
|
|
|
loop:
|
2011-06-03 12:35:42 -06:00
|
|
|
for _, m := range msgs {
|
2013-02-19 16:18:04 -07:00
|
|
|
switch m := m.(type) {
|
2011-06-03 12:35:42 -06:00
|
|
|
case *syscall.InterfaceMessage:
|
2013-02-19 16:18:04 -07:00
|
|
|
if ifindex == 0 || ifindex == int(m.Header.Index) {
|
|
|
|
ifi, err := newLink(m)
|
2011-06-03 12:35:42 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-02-27 22:58:41 -07:00
|
|
|
ift = append(ift, *ifi)
|
|
|
|
if ifindex == int(m.Header.Index) {
|
|
|
|
break loop
|
|
|
|
}
|
2011-06-03 12:35:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ift, nil
|
|
|
|
}
|
|
|
|
|
2013-02-27 22:58:41 -07:00
|
|
|
func newLink(m *syscall.InterfaceMessage) (*Interface, error) {
|
2011-12-21 05:39:00 -07:00
|
|
|
sas, err := syscall.ParseRoutingSockaddr(m)
|
|
|
|
if err != nil {
|
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
|
|
|
return nil, os.NewSyscallError("parseroutingsockaddr", err)
|
2011-06-03 12:35:42 -06:00
|
|
|
}
|
2013-02-27 22:58:41 -07:00
|
|
|
ifi := &Interface{Index: int(m.Header.Index), Flags: linkFlags(m.Header.Flags)}
|
2015-02-09 20:24:11 -07:00
|
|
|
sa, _ := sas[syscall.RTAX_IFP].(*syscall.SockaddrDatalink)
|
|
|
|
if sa != nil {
|
|
|
|
// NOTE: SockaddrDatalink.Data is minimum work area,
|
|
|
|
// can be larger.
|
|
|
|
m.Data = m.Data[unsafe.Offsetof(sa.Data):]
|
|
|
|
var name [syscall.IFNAMSIZ]byte
|
|
|
|
for i := 0; i < int(sa.Nlen); i++ {
|
|
|
|
name[i] = byte(m.Data[i])
|
2011-06-03 12:35:42 -06:00
|
|
|
}
|
2015-02-09 20:24:11 -07:00
|
|
|
ifi.Name = string(name[:sa.Nlen])
|
|
|
|
ifi.MTU = int(m.Header.Data.Mtu)
|
|
|
|
addr := make([]byte, sa.Alen)
|
|
|
|
for i := 0; i < int(sa.Alen); i++ {
|
|
|
|
addr[i] = byte(m.Data[int(sa.Nlen)+i])
|
|
|
|
}
|
|
|
|
ifi.HardwareAddr = addr[:sa.Alen]
|
2011-06-03 12:35:42 -06:00
|
|
|
}
|
2013-02-27 22:58:41 -07:00
|
|
|
return ifi, nil
|
2011-06-03 12:35:42 -06:00
|
|
|
}
|
|
|
|
|
2011-06-14 11:32:52 -06:00
|
|
|
func linkFlags(rawFlags int32) Flags {
|
|
|
|
var f Flags
|
|
|
|
if rawFlags&syscall.IFF_UP != 0 {
|
|
|
|
f |= FlagUp
|
|
|
|
}
|
|
|
|
if rawFlags&syscall.IFF_BROADCAST != 0 {
|
|
|
|
f |= FlagBroadcast
|
|
|
|
}
|
|
|
|
if rawFlags&syscall.IFF_LOOPBACK != 0 {
|
|
|
|
f |= FlagLoopback
|
|
|
|
}
|
|
|
|
if rawFlags&syscall.IFF_POINTOPOINT != 0 {
|
|
|
|
f |= FlagPointToPoint
|
|
|
|
}
|
|
|
|
if rawFlags&syscall.IFF_MULTICAST != 0 {
|
|
|
|
f |= FlagMulticast
|
|
|
|
}
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2013-02-27 22:58:41 -07:00
|
|
|
// If the ifi is nil, interfaceAddrTable returns addresses for all
|
|
|
|
// network interfaces. Otherwise it returns addresses for a specific
|
|
|
|
// interface.
|
|
|
|
func interfaceAddrTable(ifi *Interface) ([]Addr, error) {
|
|
|
|
index := 0
|
|
|
|
if ifi != nil {
|
|
|
|
index = ifi.Index
|
|
|
|
}
|
2015-12-05 00:28:25 -07:00
|
|
|
tab, err := syscall.RouteRIB(syscall.NET_RT_IFLIST, index)
|
2011-12-21 05:39:00 -07:00
|
|
|
if err != nil {
|
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
|
|
|
return nil, os.NewSyscallError("routerib", err)
|
2011-06-03 12:35:42 -06:00
|
|
|
}
|
2011-12-21 05:39:00 -07:00
|
|
|
msgs, err := syscall.ParseRoutingMessage(tab)
|
|
|
|
if err != nil {
|
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
|
|
|
return nil, os.NewSyscallError("parseroutingmessage", err)
|
2011-06-03 12:35:42 -06:00
|
|
|
}
|
2013-02-27 22:58:41 -07:00
|
|
|
var ift []Interface
|
|
|
|
if index == 0 {
|
|
|
|
ift, err = parseInterfaceTable(index, msgs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2012-02-02 15:40:03 -07:00
|
|
|
var ifat []Addr
|
2011-06-03 12:35:42 -06:00
|
|
|
for _, m := range msgs {
|
2013-02-19 16:18:04 -07:00
|
|
|
switch m := m.(type) {
|
2011-06-03 12:35:42 -06:00
|
|
|
case *syscall.InterfaceAddrMessage:
|
2013-02-27 22:58:41 -07:00
|
|
|
if index == 0 || index == int(m.Header.Index) {
|
|
|
|
if index == 0 {
|
|
|
|
var err error
|
|
|
|
ifi, err = interfaceByIndex(ift, int(m.Header.Index))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ifa, err := newAddr(ifi, m)
|
2011-06-03 12:35:42 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-01-22 15:11:22 -07:00
|
|
|
if ifa != nil {
|
|
|
|
ifat = append(ifat, ifa)
|
|
|
|
}
|
2011-06-03 12:35:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ifat, nil
|
|
|
|
}
|
|
|
|
|
2015-02-09 20:24:11 -07:00
|
|
|
func newAddr(ifi *Interface, m *syscall.InterfaceAddrMessage) (*IPNet, error) {
|
2011-12-21 05:39:00 -07:00
|
|
|
sas, err := syscall.ParseRoutingSockaddr(m)
|
|
|
|
if err != nil {
|
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
|
|
|
return nil, os.NewSyscallError("parseroutingsockaddr", err)
|
2011-06-03 12:35:42 -06:00
|
|
|
}
|
2012-02-02 15:40:03 -07:00
|
|
|
ifa := &IPNet{}
|
2015-02-09 20:24:11 -07:00
|
|
|
switch sa := sas[syscall.RTAX_NETMASK].(type) {
|
|
|
|
case *syscall.SockaddrInet4:
|
|
|
|
ifa.Mask = IPv4Mask(sa.Addr[0], sa.Addr[1], sa.Addr[2], sa.Addr[3])
|
|
|
|
case *syscall.SockaddrInet6:
|
|
|
|
ifa.Mask = make(IPMask, IPv6len)
|
|
|
|
copy(ifa.Mask, sa.Addr[:])
|
|
|
|
}
|
|
|
|
switch sa := sas[syscall.RTAX_IFA].(type) {
|
|
|
|
case *syscall.SockaddrInet4:
|
|
|
|
ifa.IP = IPv4(sa.Addr[0], sa.Addr[1], sa.Addr[2], sa.Addr[3])
|
|
|
|
case *syscall.SockaddrInet6:
|
|
|
|
ifa.IP = make(IP, IPv6len)
|
|
|
|
copy(ifa.IP, sa.Addr[:])
|
2016-02-24 03:55:20 -07:00
|
|
|
// NOTE: KAME based IPv6 protocol stack usually embeds
|
2015-02-09 20:24:11 -07:00
|
|
|
// the interface index in the interface-local or
|
|
|
|
// link-local address as the kernel-internal form.
|
|
|
|
if ifa.IP.IsLinkLocalUnicast() {
|
|
|
|
ifa.IP[2], ifa.IP[3] = 0, 0
|
2011-06-03 12:35:42 -06:00
|
|
|
}
|
|
|
|
}
|
2015-02-09 20:24:11 -07:00
|
|
|
if ifa.IP == nil || ifa.Mask == nil {
|
|
|
|
return nil, nil // Sockaddrs contain syscall.SockaddrDatalink on NetBSD
|
|
|
|
}
|
2011-12-21 05:39:00 -07:00
|
|
|
return ifa, nil
|
2011-06-03 12:35:42 -06:00
|
|
|
}
|