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 (
|
|
|
|
"syscall"
|
2016-04-23 07:36:41 -06:00
|
|
|
|
2016-07-13 10:49:48 -06:00
|
|
|
"golang_org/x/net/route"
|
2011-06-03 12:35:42 -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-03 12:35:42 -06:00
|
|
|
// interface.
|
2011-11-01 20:05:34 -06:00
|
|
|
func interfaceTable(ifindex int) ([]Interface, error) {
|
2016-04-23 07:36:41 -06:00
|
|
|
msgs, err := interfaceMessages(ifindex)
|
2011-12-21 05:39:00 -07:00
|
|
|
if err != nil {
|
2016-04-23 07:36:41 -06:00
|
|
|
return nil, err
|
2011-06-03 12:35:42 -06:00
|
|
|
}
|
2016-04-23 07:36:41 -06:00
|
|
|
n := len(msgs)
|
|
|
|
if ifindex != 0 {
|
|
|
|
n = 1
|
|
|
|
}
|
|
|
|
ift := make([]Interface, n)
|
|
|
|
n = 0
|
2011-06-03 12:35:42 -06:00
|
|
|
for _, m := range msgs {
|
2013-02-19 16:18:04 -07:00
|
|
|
switch m := m.(type) {
|
2016-04-23 07:36:41 -06:00
|
|
|
case *route.InterfaceMessage:
|
|
|
|
if ifindex != 0 && ifindex != m.Index {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ift[n].Index = m.Index
|
|
|
|
ift[n].Name = m.Name
|
|
|
|
ift[n].Flags = linkFlags(m.Flags)
|
|
|
|
if sa, ok := m.Addrs[syscall.RTAX_IFP].(*route.LinkAddr); ok && len(sa.Addr) > 0 {
|
|
|
|
ift[n].HardwareAddr = make([]byte, len(sa.Addr))
|
|
|
|
copy(ift[n].HardwareAddr, sa.Addr)
|
|
|
|
}
|
|
|
|
for _, sys := range m.Sys() {
|
|
|
|
if imx, ok := sys.(*route.InterfaceMetrics); ok {
|
|
|
|
ift[n].MTU = imx.MTU
|
|
|
|
break
|
2013-02-27 22:58:41 -07:00
|
|
|
}
|
2011-06-03 12:35:42 -06:00
|
|
|
}
|
2016-04-23 07:36:41 -06:00
|
|
|
n++
|
|
|
|
if ifindex == m.Index {
|
|
|
|
return ift[:n], nil
|
|
|
|
}
|
2011-06-03 12:35:42 -06:00
|
|
|
}
|
|
|
|
}
|
2016-04-23 07:36:41 -06:00
|
|
|
return ift[:n], nil
|
2011-06-03 12:35:42 -06:00
|
|
|
}
|
|
|
|
|
2016-04-23 07:36:41 -06:00
|
|
|
func linkFlags(rawFlags int) Flags {
|
2011-06-14 11:32:52 -06:00
|
|
|
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
|
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) {
|
|
|
|
index := 0
|
|
|
|
if ifi != nil {
|
|
|
|
index = ifi.Index
|
|
|
|
}
|
2016-04-23 07:36:41 -06:00
|
|
|
msgs, err := interfaceMessages(index)
|
2011-12-21 05:39:00 -07:00
|
|
|
if err != nil {
|
2016-04-23 07:36:41 -06:00
|
|
|
return nil, err
|
2011-06-03 12:35:42 -06:00
|
|
|
}
|
2016-04-23 07:36:41 -06:00
|
|
|
ifat := make([]Addr, 0, len(msgs))
|
2011-06-03 12:35:42 -06:00
|
|
|
for _, m := range msgs {
|
2013-02-19 16:18:04 -07:00
|
|
|
switch m := m.(type) {
|
2016-04-23 07:36:41 -06:00
|
|
|
case *route.InterfaceAddrMessage:
|
|
|
|
if index != 0 && index != m.Index {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var mask IPMask
|
|
|
|
switch sa := m.Addrs[syscall.RTAX_NETMASK].(type) {
|
|
|
|
case *route.Inet4Addr:
|
|
|
|
mask = IPv4Mask(sa.IP[0], sa.IP[1], sa.IP[2], sa.IP[3])
|
|
|
|
case *route.Inet6Addr:
|
|
|
|
mask = make(IPMask, IPv6len)
|
|
|
|
copy(mask, sa.IP[:])
|
|
|
|
}
|
|
|
|
var ip IP
|
|
|
|
switch sa := m.Addrs[syscall.RTAX_IFA].(type) {
|
|
|
|
case *route.Inet4Addr:
|
|
|
|
ip = IPv4(sa.IP[0], sa.IP[1], sa.IP[2], sa.IP[3])
|
|
|
|
case *route.Inet6Addr:
|
|
|
|
ip = make(IP, IPv6len)
|
|
|
|
copy(ip, sa.IP[:])
|
|
|
|
}
|
|
|
|
if ip != nil && mask != nil { // NetBSD may contain route.LinkAddr
|
|
|
|
ifat = append(ifat, &IPNet{IP: ip, Mask: mask})
|
2011-06-03 12:35:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ifat, nil
|
|
|
|
}
|