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.
|
|
|
|
|
|
|
|
package net
|
|
|
|
|
2016-04-12 15:19:53 -06:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
2011-06-03 12:35:42 -06:00
|
|
|
|
2016-08-21 14:52:15 -06:00
|
|
|
// BUG(mikio): On NaCl, Plan9 and Solaris, methods and functions
|
|
|
|
// related to Interface are not implemented.
|
|
|
|
|
|
|
|
// BUG(mikio): On DragonFly BSD, NetBSD and OpenBSD, the
|
|
|
|
// MulticastAddrs method of Interface is not implemented.
|
|
|
|
|
2012-01-10 17:53:32 -07:00
|
|
|
var (
|
2014-04-07 15:14:19 -06: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-01-10 17:53:32 -07:00
|
|
|
)
|
|
|
|
|
2011-06-03 12:35:42 -06:00
|
|
|
// Interface represents a mapping between network interface name
|
2016-03-01 16:21:55 -07:00
|
|
|
// and index. It also represents network interface facility
|
2011-06-03 12:35:42 -06: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
|
2011-06-14 11:32:52 -06:00
|
|
|
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
|
2011-06-03 12:35:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Addrs returns interface addresses for a specific interface.
|
2011-11-01 20:05:34 -06:00
|
|
|
func (ifi *Interface) Addrs() ([]Addr, error) {
|
2011-06-03 12:35:42 -06:00
|
|
|
if ifi == nil {
|
2015-04-21 07:53:47 -06:00
|
|
|
return nil, &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: errInvalidInterface}
|
2011-06-03 12:35:42 -06:00
|
|
|
}
|
2015-04-19 07:17:08 -06:00
|
|
|
ifat, err := interfaceAddrTable(ifi)
|
|
|
|
if err != nil {
|
2015-04-21 07:53:47 -06:00
|
|
|
err = &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: err}
|
2015-04-19 07:17:08 -06:00
|
|
|
}
|
|
|
|
return ifat, err
|
2011-06-03 12:35:42 -06:00
|
|
|
}
|
|
|
|
|
2011-08-03 22:22:52 -06:00
|
|
|
// MulticastAddrs returns multicast, joined group addresses for
|
|
|
|
// a specific interface.
|
2011-11-01 20:05:34 -06:00
|
|
|
func (ifi *Interface) MulticastAddrs() ([]Addr, error) {
|
2011-08-03 22:22:52 -06:00
|
|
|
if ifi == nil {
|
2015-04-21 07:53:47 -06:00
|
|
|
return nil, &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: errInvalidInterface}
|
2015-04-19 07:17:08 -06:00
|
|
|
}
|
|
|
|
ifat, err := interfaceMulticastAddrTable(ifi)
|
|
|
|
if err != nil {
|
2015-04-21 07:53:47 -06:00
|
|
|
err = &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: err}
|
2011-08-03 22:22:52 -06:00
|
|
|
}
|
2015-04-19 07:17:08 -06:00
|
|
|
return ifat, err
|
2011-08-03 22:22:52 -06:00
|
|
|
}
|
|
|
|
|
2012-03-08 19:50:38 -07:00
|
|
|
// Interfaces returns a list of the system's network interfaces.
|
2011-11-01 20:05:34 -06:00
|
|
|
func Interfaces() ([]Interface, error) {
|
2015-04-19 07:17:08 -06:00
|
|
|
ift, err := interfaceTable(0)
|
|
|
|
if err != nil {
|
2016-04-12 15:19:53 -06:00
|
|
|
return nil, &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: err}
|
2015-04-19 07:17:08 -06:00
|
|
|
}
|
2016-04-12 15:19:53 -06:00
|
|
|
if len(ift) != 0 {
|
|
|
|
zoneCache.update(ift)
|
|
|
|
}
|
|
|
|
return ift, nil
|
2011-06-03 12:35:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// InterfaceAddrs returns a list of the system's network interface
|
|
|
|
// addresses.
|
2011-11-01 20:05:34 -06:00
|
|
|
func InterfaceAddrs() ([]Addr, error) {
|
2015-04-19 07:17:08 -06:00
|
|
|
ifat, err := interfaceAddrTable(nil)
|
|
|
|
if err != nil {
|
2015-04-21 07:53:47 -06:00
|
|
|
err = &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: err}
|
2015-04-19 07:17:08 -06:00
|
|
|
}
|
|
|
|
return ifat, err
|
2011-06-03 12:35:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// InterfaceByIndex returns the interface specified by index.
|
2011-11-01 20:05:34 -06:00
|
|
|
func InterfaceByIndex(index int) (*Interface, error) {
|
2011-06-03 12:35:42 -06:00
|
|
|
if index <= 0 {
|
2015-04-21 07:53:47 -06:00
|
|
|
return nil, &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: errInvalidInterfaceIndex}
|
2011-06-03 12:35:42 -06:00
|
|
|
}
|
|
|
|
ift, err := interfaceTable(index)
|
|
|
|
if err != nil {
|
2015-04-21 07:53:47 -06:00
|
|
|
return nil, &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: err}
|
2011-06-03 12:35:42 -06:00
|
|
|
}
|
2015-04-19 07:17:08 -06:00
|
|
|
ifi, err := interfaceByIndex(ift, index)
|
|
|
|
if err != nil {
|
2015-04-21 07:53:47 -06:00
|
|
|
err = &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: err}
|
2015-04-19 07:17:08 -06:00
|
|
|
}
|
|
|
|
return ifi, err
|
2013-02-27 22:58:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func interfaceByIndex(ift []Interface, index int) (*Interface, error) {
|
2011-06-03 12:35:42 -06:00
|
|
|
for _, ifi := range ift {
|
2013-02-27 22:58:41 -07:00
|
|
|
if index == ifi.Index {
|
|
|
|
return &ifi, nil
|
|
|
|
}
|
2011-06-03 12:35:42 -06:00
|
|
|
}
|
2012-01-10 17:53:32 -07:00
|
|
|
return nil, errNoSuchInterface
|
2011-06-03 12:35:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// InterfaceByName returns the interface specified by name.
|
2011-11-01 20:05:34 -06:00
|
|
|
func InterfaceByName(name string) (*Interface, error) {
|
2011-06-03 12:35:42 -06:00
|
|
|
if name == "" {
|
2015-04-21 07:53:47 -06:00
|
|
|
return nil, &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: errInvalidInterfaceName}
|
2011-06-03 12:35:42 -06:00
|
|
|
}
|
|
|
|
ift, err := interfaceTable(0)
|
|
|
|
if err != nil {
|
2015-04-21 07:53:47 -06:00
|
|
|
return nil, &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: err}
|
2011-06-03 12:35:42 -06:00
|
|
|
}
|
2016-04-12 15:19:53 -06:00
|
|
|
if len(ift) != 0 {
|
|
|
|
zoneCache.update(ift)
|
|
|
|
}
|
2011-06-03 12:35:42 -06:00
|
|
|
for _, ifi := range ift {
|
|
|
|
if name == ifi.Name {
|
|
|
|
return &ifi, nil
|
|
|
|
}
|
|
|
|
}
|
2015-04-21 07:53:47 -06:00
|
|
|
return nil, &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: errNoSuchInterface}
|
2011-06-03 12:35:42 -06:00
|
|
|
}
|
2016-04-12 15:19:53 -06:00
|
|
|
|
|
|
|
// An ipv6ZoneCache represents a cache holding partial network
|
|
|
|
// interface information. It is used for reducing the cost of IPv6
|
|
|
|
// addressing scope zone resolution.
|
|
|
|
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),
|
|
|
|
}
|
|
|
|
|
|
|
|
func (zc *ipv6ZoneCache) update(ift []Interface) {
|
|
|
|
zc.Lock()
|
|
|
|
defer zc.Unlock()
|
|
|
|
now := time.Now()
|
|
|
|
if zc.lastFetched.After(now.Add(-60 * time.Second)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
zc.lastFetched = now
|
|
|
|
if len(ift) == 0 {
|
|
|
|
var err error
|
|
|
|
if ift, err = interfaceTable(0); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
zc.toName[ifi.Index] = ifi.Name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func zoneToString(zone int) string {
|
|
|
|
if zone == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
zoneCache.update(nil)
|
|
|
|
zoneCache.RLock()
|
|
|
|
defer zoneCache.RUnlock()
|
|
|
|
name, ok := zoneCache.toName[zone]
|
|
|
|
if !ok {
|
|
|
|
name = uitoa(uint(zone))
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
|
|
|
func zoneToInt(zone string) int {
|
|
|
|
if zone == "" {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
zoneCache.update(nil)
|
|
|
|
zoneCache.RLock()
|
|
|
|
defer zoneCache.RUnlock()
|
|
|
|
index, ok := zoneCache.toIndex[zone]
|
|
|
|
if !ok {
|
2016-08-16 10:33:27 -06:00
|
|
|
index, _, _ = dtoi(zone)
|
2016-04-12 15:19:53 -06:00
|
|
|
}
|
|
|
|
return index
|
|
|
|
}
|