Retro68/gcc/libgo/go/net/interface.go

257 lines
7.2 KiB
Go
Raw Normal View History

// Copyright 2011 The Go Authors. All rights reserved.
2012-03-27 23:13:14 +00:00
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package net
import (
"errors"
"sync"
"time"
)
2019-06-02 15:48:37 +00:00
// BUG(mikio): On JS and NaCl, methods and functions related to
// Interface are not implemented.
2019-06-02 15:48:37 +00:00
// BUG(mikio): On AIX, DragonFly BSD, NetBSD, OpenBSD, Plan 9 and
// Solaris, the MulticastAddrs method of Interface is not implemented.
2012-03-27 23:13:14 +00:00
var (
2015-08-28 15:33:40 +00:00
errInvalidInterface = errors.New("invalid network interface")
errInvalidInterfaceIndex = errors.New("invalid network interface index")
errInvalidInterfaceName = errors.New("invalid network interface name")
errNoSuchInterface = errors.New("no such network interface")
errNoSuchMulticastInterface = errors.New("no such multicast network interface")
2012-03-27 23:13:14 +00:00
)
// Interface represents a mapping between network interface name
// and index. It also represents network interface facility
2012-03-27 23:13:14 +00:00
// information.
type Interface struct {
Index int // positive integer that starts at one, zero is never used
MTU int // maximum transmission unit
Name string // e.g., "en0", "lo0", "eth0.100"
HardwareAddr HardwareAddr // IEEE MAC-48, EUI-48 and EUI-64 form
Flags Flags // e.g., FlagUp, FlagLoopback, FlagMulticast
}
type Flags uint
const (
FlagUp Flags = 1 << iota // interface is up
FlagBroadcast // interface supports broadcast access capability
FlagLoopback // interface is a loopback interface
FlagPointToPoint // interface belongs to a point-to-point link
FlagMulticast // interface supports multicast access capability
)
var flagNames = []string{
"up",
"broadcast",
"loopback",
"pointtopoint",
"multicast",
}
func (f Flags) String() string {
s := ""
for i, name := range flagNames {
if f&(1<<uint(i)) != 0 {
if s != "" {
s += "|"
}
s += name
}
}
if s == "" {
s = "0"
}
return s
}
// Addrs returns a list of unicast interface addresses for a specific
// interface.
2012-03-27 23:13:14 +00:00
func (ifi *Interface) Addrs() ([]Addr, error) {
if ifi == nil {
2017-04-10 11:32:00 +00:00
return nil, &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: errInvalidInterface}
2012-03-27 23:13:14 +00:00
}
2017-04-10 11:32:00 +00:00
ifat, err := interfaceAddrTable(ifi)
if err != nil {
err = &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: err}
}
return ifat, err
2012-03-27 23:13:14 +00:00
}
// MulticastAddrs returns a list of multicast, joined group addresses
// for a specific interface.
2012-03-27 23:13:14 +00:00
func (ifi *Interface) MulticastAddrs() ([]Addr, error) {
if ifi == nil {
2017-04-10 11:32:00 +00:00
return nil, &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: errInvalidInterface}
}
ifat, err := interfaceMulticastAddrTable(ifi)
if err != nil {
err = &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: err}
2012-03-27 23:13:14 +00:00
}
2017-04-10 11:32:00 +00:00
return ifat, err
2012-03-27 23:13:14 +00:00
}
2014-09-21 17:33:12 +00:00
// Interfaces returns a list of the system's network interfaces.
2012-03-27 23:13:14 +00:00
func Interfaces() ([]Interface, error) {
2017-04-10 11:32:00 +00:00
ift, err := interfaceTable(0)
if err != nil {
return nil, &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: err}
}
if len(ift) != 0 {
2019-06-02 15:48:37 +00:00
zoneCache.update(ift, false)
2017-04-10 11:32:00 +00:00
}
return ift, nil
2012-03-27 23:13:14 +00:00
}
// InterfaceAddrs returns a list of the system's unicast interface
2012-03-27 23:13:14 +00:00
// addresses.
//
// The returned list does not identify the associated interface; use
// Interfaces and Interface.Addrs for more detail.
2012-03-27 23:13:14 +00:00
func InterfaceAddrs() ([]Addr, error) {
2017-04-10 11:32:00 +00:00
ifat, err := interfaceAddrTable(nil)
if err != nil {
err = &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: err}
}
return ifat, err
2012-03-27 23:13:14 +00:00
}
// InterfaceByIndex returns the interface specified by index.
//
// On Solaris, it returns one of the logical network interfaces
// sharing the logical data link; for more precision use
// InterfaceByName.
2012-03-27 23:13:14 +00:00
func InterfaceByIndex(index int) (*Interface, error) {
if index <= 0 {
2017-04-10 11:32:00 +00:00
return nil, &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: errInvalidInterfaceIndex}
2012-03-27 23:13:14 +00:00
}
ift, err := interfaceTable(index)
if err != nil {
2017-04-10 11:32:00 +00:00
return nil, &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: err}
2012-03-27 23:13:14 +00:00
}
2017-04-10 11:32:00 +00:00
ifi, err := interfaceByIndex(ift, index)
if err != nil {
err = &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: err}
}
return ifi, err
2014-09-21 17:33:12 +00:00
}
func interfaceByIndex(ift []Interface, index int) (*Interface, error) {
2012-03-27 23:13:14 +00:00
for _, ifi := range ift {
2014-09-21 17:33:12 +00:00
if index == ifi.Index {
return &ifi, nil
}
2012-03-27 23:13:14 +00:00
}
return nil, errNoSuchInterface
}
// InterfaceByName returns the interface specified by name.
func InterfaceByName(name string) (*Interface, error) {
if name == "" {
2017-04-10 11:32:00 +00:00
return nil, &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: errInvalidInterfaceName}
2012-03-27 23:13:14 +00:00
}
ift, err := interfaceTable(0)
if err != nil {
2017-04-10 11:32:00 +00:00
return nil, &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: err}
2012-03-27 23:13:14 +00:00
}
if len(ift) != 0 {
2019-06-02 15:48:37 +00:00
zoneCache.update(ift, false)
}
2012-03-27 23:13:14 +00:00
for _, ifi := range ift {
if name == ifi.Name {
return &ifi, nil
}
}
2017-04-10 11:32:00 +00:00
return nil, &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: errNoSuchInterface}
2012-03-27 23:13:14 +00:00
}
// An ipv6ZoneCache represents a cache holding partial network
// interface information. It is used for reducing the cost of IPv6
// addressing scope zone resolution.
//
// Multiple names sharing the index are managed by first-come
// first-served basis for consistency.
type ipv6ZoneCache struct {
sync.RWMutex // guard the following
lastFetched time.Time // last time routing information was fetched
toIndex map[string]int // interface name to its index
toName map[int]string // interface index to its name
}
var zoneCache = ipv6ZoneCache{
toIndex: make(map[string]int),
toName: make(map[int]string),
}
2019-06-02 15:48:37 +00:00
// update refreshes the network interface information if the cache was last
// updated more than 1 minute ago, or if force is set. It reports whether the
// cache was updated.
func (zc *ipv6ZoneCache) update(ift []Interface, force bool) (updated bool) {
zc.Lock()
defer zc.Unlock()
now := time.Now()
2019-06-02 15:48:37 +00:00
if !force && zc.lastFetched.After(now.Add(-60*time.Second)) {
return false
}
zc.lastFetched = now
if len(ift) == 0 {
var err error
if ift, err = interfaceTable(0); err != nil {
2019-06-02 15:48:37 +00:00
return false
}
}
zc.toIndex = make(map[string]int, len(ift))
zc.toName = make(map[int]string, len(ift))
for _, ifi := range ift {
zc.toIndex[ifi.Name] = ifi.Index
if _, ok := zc.toName[ifi.Index]; !ok {
zc.toName[ifi.Index] = ifi.Name
}
}
2019-06-02 15:48:37 +00:00
return true
}
2018-12-28 15:30:48 +00:00
func (zc *ipv6ZoneCache) name(index int) string {
if index == 0 {
return ""
}
2019-06-02 15:48:37 +00:00
updated := zoneCache.update(nil, false)
zoneCache.RLock()
2018-12-28 15:30:48 +00:00
name, ok := zoneCache.toName[index]
2019-06-02 15:48:37 +00:00
zoneCache.RUnlock()
if !ok && !updated {
zoneCache.update(nil, true)
zoneCache.RLock()
name, ok = zoneCache.toName[index]
zoneCache.RUnlock()
}
if !ok { // last resort
2018-12-28 15:30:48 +00:00
name = uitoa(uint(index))
}
return name
}
2018-12-28 15:30:48 +00:00
func (zc *ipv6ZoneCache) index(name string) int {
if name == "" {
return 0
}
2019-06-02 15:48:37 +00:00
updated := zoneCache.update(nil, false)
zoneCache.RLock()
2018-12-28 15:30:48 +00:00
index, ok := zoneCache.toIndex[name]
2019-06-02 15:48:37 +00:00
zoneCache.RUnlock()
if !ok && !updated {
zoneCache.update(nil, true)
zoneCache.RLock()
index, ok = zoneCache.toIndex[name]
zoneCache.RUnlock()
}
if !ok { // last resort
2018-12-28 15:30:48 +00:00
index, _, _ = dtoi(name)
}
return index
}